[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String to Object name?

Mariko C.

3/3/2008 11:04:00 PM

I'd like to change a string, for example, "something" and turn it into
an object reference. For example:

I have string "something," now I'd like to take that and turn it into

something = "some other string"

Is this possible? Thanks in advance for any help.
--
Posted via http://www.ruby-....

5 Answers

Greg Donald

3/3/2008 11:19:00 PM

0

On 3/3/08, Mariko C. <lessthaneloquent@yahoo.com> wrote:
> I'd like to change a string, for example, "something" and turn it into
> an object reference. For example:
>
> I have string "something," now I'd like to take that and turn it into
>
> something = "some other string"
>
> Is this possible? Thanks in advance for any help.


I'm not sure how you'd do it otherwise, but using Rails I do it like this:

c = "foo".singularize.camelize.constantize
bar = c.new


--
Greg Donald
http://des...

Joshua Ballanco

3/4/2008 12:43:00 AM

0

Mariko C. wrote:
> I'd like to change a string, for example, "something" and turn it into
> an object reference. For example:
>
> I have string "something," now I'd like to take that and turn it into
>
> something = "some other string"
>
> Is this possible? Thanks in advance for any help.

Meta-programming to the rescue!

#!/usr/bin/env ruby

a = "aString"

eval(a + " = 5")

puts aString
--
Posted via http://www.ruby-....

Christopher Swasey

3/4/2008 12:45:00 AM

0

On 3/3/08, Mariko C. <lessthaneloquent@yahoo.com> wrote:
> I'd like to change a string, for example, "something" and turn it into
> an object reference. For example:
>
> I have string "something," now I'd like to take that and turn it into
>
> something = "some other string"
>
> Is this possible? Thanks in advance for any help.

If I understand you properly, you have the name of a variable in a
string, and you want to use that string to fetch the actual variable
by that name.

What you want is Object#instance_variable_get:
@a = "@b"
@b = ["this is a test"]
instance_variable_get(@a)
=> ["this is a test"]

Note that the @ is important. You can always prepend it as needed:
instance_variable_get("@" + @a)

Christopher

Mariko C.

3/4/2008 7:51:00 AM

0

Joshua Ballanco wrote:
> Meta-programming to the rescue!
>
> #!/usr/bin/env ruby
>
> a = "aString"
>
> eval(a + " = 5")
>
> puts aString


Hi Joshua. I tried this through irb and it works fine, but it gives me
an "undefined local variable" error through Ruby on Rails. Any idea how
to get this to work?
--
Posted via http://www.ruby-....

Arlen Cuss

3/4/2008 9:21:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Tue, Mar 4, 2008 at 6:50 PM, Mariko C. <lessthaneloquent@yahoo.com>
wrote:

> Hi Joshua. I tried this through irb and it works fine, but it gives me
> an "undefined local variable" error through Ruby on Rails. Any idea how
> to get this to work?


Can you show us the code? Possibly a variable name was misspelled.

Arlen