creating and naming variables dynamic

T

Tarscher

Hi all,

I want to create a variable dynamic. I have defined some classes and
depending on data I get from a database I need to construct an object
eg:

now I do

if type=="car"
variab = Car.new
elsif type="boat"
variab = Boat.new
end

can I do something like
variab = Instance.new(type)


Following this: can I give dynamic variablenames?
now I do
if type=="car"
car_string = Car.new
elsif type="boat"
boat_string = Boat.new
end

can I do something like
dynamicpart_string = Instance.new(type)

Regards,
Stijn
 
D

David A. Black

Hi --

Hi all,

I want to create a variable dynamic. I have defined some classes and
depending on data I get from a database I need to construct an object
eg:

now I do

if type=="car"
variab = Car.new
elsif type="boat"
variab = Boat.new
end

can I do something like
variab = Instance.new(type)


Following this: can I give dynamic variablenames?
now I do
if type=="car"
car_string = Car.new
elsif type="boat"
boat_string = Boat.new
end

can I do something like
dynamicpart_string = Instance.new(type)

Fred Cheung has answered this post for you on the Rails mailing list.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!
 
P

Peter Szinek

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

if type can be either 'car' or 'boat' i.e. lowercase strings:

my_class = Object.const_get(type.capitalize)

and then

my_class.new

etc.

alternatively, you can look into active_support, where you have stuff
like
=> "aa"

HTH,
Peter
___
http://www.rubyrailways.com
http://scrubyt.org
 
R

Robert Klemme

Hi all,

I want to create a variable dynamic. I have defined some classes and
depending on data I get from a database I need to construct an object
eg:

now I do

if type=="car"
variab = Car.new
elsif type="boat"
variab = Boat.new
end

can I do something like
variab = Instance.new(type)

More options

factory = {
"car" => Car,
"boat" => Boat,
}

var = factory[type].new
Following this: can I give dynamic variablenames?
now I do
if type=="car"
car_string = Car.new
elsif type="boat"
boat_string = Boat.new
end

can I do something like
dynamicpart_string = Instance.new(type)

Better use a Hash for this

vars = {}
vars[type] = factory[type].new

Cheers

robert
 
B

Bernardo Monteiro Rufino

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

You can try methods instead of dynamic variables, but the best option is the
one above with hash.

self.class.send:)define_method, type){Object.const_get(type.capitalize);}
 

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,206
Messages
2,571,070
Members
47,676
Latest member
scazeho

Latest Threads

Top