1

I'm using Ruby 1.9.2 and need to go through all of the values for a table to make sure everything is in UTF-8 encoding. There are a lot of columns so I was hoping to be able to use the column_names method to loop through them all and encode the values to UTF-8. I thought this might work:

def self.make_utf
  for listing in Listing.all
    for column in Listing.column_names
      column_value_utf = listing.send(column.to_sym).encode('UTF-8')
      listing.send(column.to_sym) = column_value_utf
    end
    listing.save
  end

  return "Updated columns to UTF-8"

end

But it returns an error:

syntax error, unexpected '=', expecting keyword_end
        listing.send(column.to_sym) = column_value_utf

I can't figure out how to make this work correctly.

Frank
  • 714
  • 1
  • 9
  • 21

1 Answers1

9

You're using send wrong and you're sending the wrong symbol for what you want to do:

listing.send(column + '=', column_value_utf)

You're trying to call the x= method (for some x) with column_value_utf as an argument, that's what o.x = column_value_utf would normally do. So you need to build the right method name (just a string will do) and then send the arguments for that method in as arguments to send.

mu is too short
  • 426,620
  • 70
  • 833
  • 800