Hi.
I've a little problem. How can I use a string use as an object name?
That works well:
puts p.name # -> dirk
But how can I use the string s as object name, I tried eval but I the result is not good. Where is my mistake. Can anybody help me?
s = 'name'
puts eval "p.s"
greetings
Dirk Einecke
Did you mean,
puts eval "p s" # => "name"
···
--- Dirk Einecke <dirk.einecke@gmx.de> wrote:
Hi.
I've a little problem. How can I use a string use as an object name?
That works well:
puts p.name # -> dirk
But how can I use the string s as object name, I tried eval but I the
result is not good. Where is my mistake. Can anybody help me?
s = 'name'
puts eval "p.s"
__________________________________
Do you Yahoo!?
Friends. Fun. Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/
Mark10
(Mark)
13 June 2004 19:51
3
Dirk Einecke wrote:
Hi.
I've a little problem. How can I use a string use as an object name?
That works well:
puts p.name # -> dirk
But how can I use the string s as object name, I tried eval but I the result is not good. Where is my mistake. Can anybody help me?
s = 'name'
puts eval "p.s"
since you want to put the value of s into the string that get's evaled you need to enclose it in #{ } like this
puts eval "p.#{s}"
Though depending on what you're doing a better way might be to use the send method
puts p.send(s)
···
--
Mark Sparshatt
Jeff Mitchell wrote:
Did you mean,
puts eval "p s" # => "name"
Not really...
Okay. From the scratch. I've a class to map a get params to local vars:
class CgiParamsToLocal
def initialize(params)
@params = params
end
def method_missing(m, *other)
@params [m.to_s][0]
end
end
p = CgiParamsToLocal.new($cgi.params)
Now I try to use a string as a object name like I wrote in my first posting.
greetings
Dirk Einecke
Mark Sparshatt wrote:
puts eval "p.#{s}"
Okay. That works.
Though depending on what you're doing a better way might be to use the send method
puts p.send(s)
Why is this a better way?
greetings
Dirk Einecke
Mark10
(Mark)
13 June 2004 20:55
9
Dirk Einecke wrote:
Mark Sparshatt wrote:
puts eval "p.#{s}"
Okay. That works.
Though depending on what you're doing a better way might be to use the send method
puts p.send(s)
Why is this a better way?
It's quicker since Ruby doesn't need to parse the expression string.
Also, IMHO, it better expresses the fact that you're sending the message contained in s to the object p.
···
--
Mark Sparshatt
Mark10
(Mark)
13 June 2004 21:10
10
Mark Sparshatt wrote:
Dirk Einecke wrote:
Mark Sparshatt wrote:
puts eval "p.#{s}"
Okay. That works.
Though depending on what you're doing a better way might be to use the send method
puts p.send(s)
Why is this a better way?
It's quicker since Ruby doesn't need to parse the expression string.
Also, IMHO, it better expresses the fact that you're sending the message contained in s to the object p.
I forgot to mention there are using security concerns with using eval. If s is taken from an external source then it's possible for someone to set it to
"send('system', 'rm -rf /')"
which would very quickly ruin your day.
···
--
Mark Sparshatt