Hello,
I’m a seasoned Perl programmer trying to learn Ruby.
Is there a way a variable name can be variable? Like, how would I
rewrite the following piece of Perl code in Ruby:
$a = 5;
$b = 'a';
print $$b; # should print 5
And can this be done with instance variables? I’m trying to do this:
class Series
attr_accessor :sid, :handle, :name
def initialize(vals)
vals.each_key { |key|
@(key) = vals[key] # This doesn’t work; syntax error
}
end
end
s = Series.new({“sid” => 3, “handle” => “smoon”, “name” => “Sailor Moon”})
puts s.name # should print “Sailor Moon”
Hello,
I’m a seasoned Perl programmer trying to learn Ruby.
Is there a way a variable name can be variable? Like, how would I
rewrite the following piece of Perl code in Ruby:
$a = 5;
$b = ‘a’;
print $$b; # should print 5
a = 5
b = ‘a’
puts eval b
And can this be done with instance variables? I’m trying to do this:
class Series
attr_accessor :sid, :handle, :name
def initialize(vals)
vals.each_key { |key|
@(key) = vals[key] # This doesn’t work; syntax error
I don’t understand what you are trying to do here.
/Erik
···
On Mon, 2002-06-03 at 00:49, Philip Mak wrote:
–
Erik Bågfors | erik@bagfors.nu
Supporter of free software | GSM +46 733 279 273
fingerprint: 6666 A85B 95D3 D26B 296B 6C60 4F32 2C0B 693D 6E32
Philip Mak pmak@animeglobe.com writes:
And can this be done with instance variables? I’m trying to do this:
class Series
attr_accessor :sid, :handle, :name
def initialize(vals)
vals.each_key { |key|
@(key) = vals[key] # This doesn’t work; syntax error
}
end
end
This particular case can be rewritten either with instance_eval or
method_missing. Which is less evil is up to you.
% cat series_eval.rb
class Series
attr_accessor :sid, :handle, :name
def initialize(vals)
vals.each_key { |key|
instance_eval “@#{key} = vals[key]”
}
end
end
s = Series.new({“sid” => 3, “handle” => “smoon”, “name” => “Sailor Moon”})
puts s.name # should print “Sailor Moon”
%
% ruby series_eval.rb
Sailor Moon
% cat series_missing.rb
class Series
def initialize(vals)
@vals = vals
end
def method_missing (symbol, *args)
@vals[symbol.to_s]
end
end
s = Series.new({“sid” => 3, “handle” => “smoon”, “name” => “Sailor Moon”})
puts s.name # should print “Sailor Moon”
%
% ruby series_missing.rb
Sailor Moon
%
– Ed
Basically, I want to do this:
class Series
attr_accessor :sid, :handle, :name
def initialize(vals)
@sid = vals[‘sid’]
@handle = vals[‘handle’]
@name = vals[‘name’]
end
end
without having to type out the @x = vals[‘x’] line for each and every
instance variable, since I have other classes that have a lot of
variables.
I tried eval(@key = vals[key]) and eval(@key) = vals[key] but those
didn’t evaluate correctly.
···
On Mon, Jun 03, 2002 at 07:57:43AM +0900, Erik Bågfors wrote:
class Series
attr_accessor :sid, :handle, :name
def initialize(vals)
vals.each_key { |key|
@(key) = vals[key] # This doesn’t work; syntax error
I don’t understand what you are trying to do here.
I think I got it.
class Series
attr_accessor :sid, :handle, :name
def initialize(vals)
vals.each_key { |key|
eval(“@” + key + “= vals[key]”)
}
end
end
A bit kludgy looking (I’m porting something from Perl, and it abused
the fact that classes are hashes in Perl) but it works.
···
On Mon, Jun 03, 2002 at 08:08:29AM +0900, Philip Mak wrote:
I tried eval(@key = vals[key]) and eval(@key) = vals[key] but those
didn’t evaluate correctly.
Hi,
class Series
attr_accessor :sid, :handle, :name
def initialize(vals)
vals.each_key { |key|
eval(“@” + key + “= vals[key]”)
}
end
end
A bit kludgy looking (I’m porting something from Perl, and it abused
the fact that classes are hashes in Perl) but it works.
Series = Struct.new(nil, :sid, :handle, :name)
class Series
def initialize(vals)
super(*vals.indexes(*members))
# super(*vals.select(*members)) # for 1.7
end
end
a = Series.new(“sid”=>3, “handle” => “smoon”, “name” => “Sailor Moon”)
puts a.name
···
At Mon, 3 Jun 2002 08:27:04 +0900, Philip Mak wrote:
–
Nobu Nakada