[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Dynamically naming an Object

John Gearhart

10/9/2008 2:24:00 AM

I'm new to ruby, so this may be simple, but I can not figure it out.
Simply, I am wanting to dynamically create objects at runtime with a
variable name also given at runtime.

for example -

keys = []

word = ""

while word != "end"
word = (gets.chomp)
word = word.downcase
keys.push(word) unless word == "end"
end

class Word
end

keys.each { |new_obj| new_obj = Word.obj

[user input of win]

puts win.inspect

I know it may sound strange, but I am just playing. Any help would be
greatly appreciated.

-M
--
Posted via http://www.ruby-....

1 Answer

Robert Klemme

10/9/2008 7:39:00 AM

0

2008/10/9 John Gearhart <john@johngearhart.com>:
> I'm new to ruby, so this may be simple, but I can not figure it out.
> Simply, I am wanting to dynamically create objects at runtime with a
> variable name also given at runtime.
>
> for example -
>
> keys = []
>
> word = ""
>
> while word != "end"
> word = (gets.chomp)
> word = word.downcase
> keys.push(word) unless word == "end"
> end
>
> class Word
> end
>
> keys.each { |new_obj| new_obj = Word.obj
>
> [user input of win]
>
> puts win.inspect
>
> I know it may sound strange, but I am just playing. Any help would be
> greatly appreciated.

You did not state which objects you want to dynamically generate names for...

Regardless: generating local variable names does not work as you might
expect because local variables which are not known at compile time of
the script code cannot be used easily. Rather use a Hash which seems
the more appropriate data structure in your case anyway.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end