Switch string to object

Hello,

How to switch a string to an object?
for example,

hello = “world”
puts “hello”.to_object

will print “world”.

Thanks.

Why don’t you think it’s an object already?

On Fri, Aug 19, 2011 at 1:30 AM, zuerrong [email protected] wrote:

Thanks.

Objects are just things that can be assigned to variables – you can
also
invoke methods on them, and they have classes.

So strings, like nearly everything in Ruby, are objects.

What are you expecting it to output?

sorry, it’s really an object already.
maybe I want to it’s to an object variable.

2011/8/19 Mike S. [email protected]:

Objects are just things that can be assigned to variables – you can also
invoke methods on them, and they have classes.

So strings, like nearly everything in Ruby, are objects.

What are you expecting it to output?

hello = “world”
puts “hello”.to_object_variable

will expect to print “world”.

Thanks.

2011/8/19 Josh C. [email protected]:

hello, in this case, is a variable. Notice that you cannot assign hello,
itself to a variable. Only the object that hello is pointing to (the
string "world")

Anywhere you use hello, it will be implicitly dereferenced. It is the same
as using the object that it is pointing to. So that means you simply need to
do puts hello and it will print “world”

So, no way to translate a string to a variable name? Thanks.

On Fri, Aug 19, 2011 at 1:52 AM, zuerrong [email protected] wrote:

puts “hello”.to_object_variable

will expect to print “world”.

Thanks.

hello, in this case, is a variable. Notice that you cannot assign
hello,
itself to a variable. Only the object that hello is pointing to (the
string "world")

Anywhere you use hello, it will be implicitly dereferenced. It is the
same
as using the object that it is pointing to. So that means you simply
need to
do puts hello and it will print “world”

You can just use eval().

hello = “world”
puts eval(“hello”)

This is, however, a bad idea, especially if the eval’d string comes
from the user. You should never have to do that.

– Matma R.

2011/8/19 zuerrong [email protected]:

On 08/19/2011 12:03 AM, zuerrong wrote:

So, no way to translate a string to a variable name? Thanks.

Not really any good ways, at least not for local variables. You could
always use eval though.

For constants there is Module.const_get. For instance variables there is
instance_variable_get.

-Justin

Thanks for all the helps.
I now got the idea.

2011/8/19 Bartosz Dziewoński [email protected]:

On Fri, Aug 19, 2011 at 2:03 AM, zuerrong [email protected] wrote:

as using the object that it is pointing to. So that means you simply need
to
do puts hello and it will print “world”

So, no way to translate a string to a variable name? Thanks.

If you need this feature, there is probably a better way to do whatever
it
is you’re trying to do.

And just thinking about what you might be trying to do that could
require
such a feature implies that the better way is probably to use a hash
table.
If you aren’t familiar with hashes, RKS has an introduction in session 3
(
http://ruby-kickstart.com/#session3)