R
ryanslists
Hello,
I would like to do something very simple -- assign a value to an object
attribute that has been accessed dynamically.
In Perl, this would be
my %hash = (foo => 90890, bar =>34243);
$obj->$_($hash{$_}) foreach keys %hash;
In Ruby I tried
obj.send(attr) = foo.attr
but it exploded with an error.
I found a very complicated four year old thread on comp.lang.ruby
called "dynamically assigning instance variables:"
http://groups.google.com/group/comp...name+attribute+assign&rnum=1#bebbab29a34c759e
It is 27 posts long and contains baroque syntax like
def biv(attributes)
attributes.each do |k, v|
type.senddefin, k)
send(k.to_s + "=", v)
end
end
This can't be the only way to do such a simple thing in Ruby ... or is
it? Also, if so, where can I learn more about why the equals sign is
considered part of the method name?
Thanks for any help!
Ryan Tate
I would like to do something very simple -- assign a value to an object
attribute that has been accessed dynamically.
In Perl, this would be
my %hash = (foo => 90890, bar =>34243);
$obj->$_($hash{$_}) foreach keys %hash;
In Ruby I tried
obj.send(attr) = foo.attr
but it exploded with an error.
I found a very complicated four year old thread on comp.lang.ruby
called "dynamically assigning instance variables:"
http://groups.google.com/group/comp...name+attribute+assign&rnum=1#bebbab29a34c759e
It is 27 posts long and contains baroque syntax like
def biv(attributes)
attributes.each do |k, v|
type.senddefin, k)
send(k.to_s + "=", v)
end
end
This can't be the only way to do such a simple thing in Ruby ... or is
it? Also, if so, where can I learn more about why the equals sign is
considered part of the method name?
Thanks for any help!
Ryan Tate