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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.