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?

···

--
Posted via http://www.ruby-forum.com/.

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?

···

On Fri, Aug 19, 2011 at 1:30 AM, zuerrong <zuerrong@gmail.com> wrote:

Hello,

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

hello = "world"
puts "hello".to_object

will print "world".

Thanks.

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

···

2011/8/19 Mike Stephens <rubfor@recitel.net>:

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

--
Posted via http://www.ruby-forum.com/\.

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.

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"

···

On Fri, Aug 19, 2011 at 1:52 AM, zuerrong <zuerrong@gmail.com> wrote:

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

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

···

2011/8/19 Josh Cheek <josh.cheek@gmail.com>:

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"

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

···

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

2011/8/19 Josh Cheek<josh.cheek@gmail.com>:

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.

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 Rex

···

2011/8/19 zuerrong <zuerrong@gmail.com>:

2011/8/19 Josh Cheek <josh.cheek@gmail.com>:

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.

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\)

···

On Fri, Aug 19, 2011 at 2:03 AM, zuerrong <zuerrong@gmail.com> wrote:

2011/8/19 Josh Cheek <josh.cheek@gmail.com>:

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

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

···

2011/8/19 Bartosz Dziewoński <matma.rex@gmail.com>:

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 Rex

2011/8/19 zuerrong <zuerrong@gmail.com>:

2011/8/19 Josh Cheek <josh.cheek@gmail.com>:

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.