If you have not set other local variables inside the method, local_variables will provide you with a list of method parameter names (if you set other variables, you can simply call local_variables first thing and remember the result). So you can do what you want with local_variables + eval :
class Automator def fill_specific_form(first_name, last_name) local_variables.each do |var| puts "Setting #{var} to #{eval var.to_s}" end end end Automator.new().fill_specific_form("Mads", "Mobaek")
Be an adviser that this is pure evil.
And at least for your example
puts "Setting first_name to #{first_name}" puts "Setting last_name to #{last_name}"
seems much more reasonable.
You can also do fields = {:first_name => first_name, :last_name => last_name} at the beginning of the method, and then go with your fields.each_pair code.
sepp2k Aug 11 '10 at 9:11 2010-08-11 09:11
source share