Use a string a object name

D

Dirk Einecke

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
 
J

Jeff Mitchell

--- Dirk Einecke said:
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"

Did you mean,
puts eval "p s" # => "name"





__________________________________
Do you Yahoo!?
Friends. Fun. Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/
 
M

Mark Sparshatt

Dirk said:
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)
 
M

Marek Janukowicz

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"

puts eval "p.#{s}"
 
D

Dirk Einecke

Jeff said:
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
 
M

Mark Sparshatt

Dirk said:
Okay. That works.



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.
 
M

Mark Sparshatt

Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,375
Latest member
FelishaCma

Latest Threads

Top