On Feb 16, 12:14 am, rapdup <[email protected]> wrote:
> Okay, I found a solution. It isn't very readable, but it works:
>
> user = User.new(:login=> "foo", :password=>"bar") # :password is not
> a column in the db, but defined in the model using
> "attr_accessor :password" (see initial post)
> [:login, :password].each do |x|
> getter = x # => :password
> setter = (x.to_s << "=").to_sym # => :password=
> original_val = user.method(getter).call
> user.method(setter).call(some_new_val)
> do_some_test(user)
> user.method(setter).call(original_val)
> end
>
> Any clean up suggestion?
This is marginally cleaner IMO:
[:login, :password].each do |getter|
setter = "#{getter}="
original_val = user.send(getter)
user.send(setter, some_new_val)
do_some_test(user)
user.send(setter, original_val)
end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---