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.
You’re using
sendwrong and you’re sending the wrong symbol for what you want to do:You’re trying to call the
x=method (for somex) withcolumn_value_utfas an argument, that’s whato.x = column_value_utfwould 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 tosend.