Dynamically call classes

T

Tizian Taz

Hello,

I'm new to Ruby but have got several years of experience with PHP. I
work on a project with several classes witch analyse text blocks. The
blocks begin with headers like "Status" or "Update" but that's not my
problem.

I'd like to call classes dynamically like this :

var = 'Status'
# and now I want to call the class "Status"

How can I manage it?
 
P

Peter Szinek

Tizian said:
Hello,

I'm new to Ruby but have got several years of experience with PHP. I
work on a project with several classes witch analyse text blocks. The
blocks begin with headers like "Status" or "Update" but that's not my
problem.

I'd like to call classes dynamically like this :

var = 'Status'
# and now I want to call the class "Status"

How can I manage it?
=> Status

cheers,
Peter
___
http://www.rubyrailways.com
http://scrubyt.org
 
T

Tizian Taz

Ok, but now, how can I access for example the method Hello which this
existing class inheritated from his mother-class?

I ask this because "Status.Hello" doesn't work...

Maybe I wasn't clear enough in my description :

The classes that I want to call all exist but I don't want to make a
huge amount of tests to know which I have to call. Tell me if I'm not
clear :S
 
P

Peter Szinek

Tizian said:
Ok, but now, how can I access for example the method Hello which this
existing class inheritated from his mother-class?

I ask this because "Status.Hello" doesn't work...

Maybe I wasn't clear enough in my description :

The classes that I want to call all exist but I don't want to make a
huge amount of tests to know which I have to call. Tell me if I'm not
clear :S

class Animal
def ooze
puts "oooooo"
end

def Animal.winkle
puts "winkle"
end
end

class Dog < Animal
end

Kernel.const_get("Dog").new.ooze
Kernel.const_get("Dog").winkle


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

Rick DeNatale

Ok, but now, how can I access for example the method Hello which this
existing class inheritated from his mother-class?

I ask this because "Status.Hello" doesn't work...

Maybe I wasn't clear enough in my description :

The classes that I want to call all exist but I don't want to make a
huge amount of tests to know which I have to call. Tell me if I'm not
clear :S

You can do this, and others are already helping. but...

Do you really have a firm requirement to use a string to represent the class.

Why not just:

var = Status

...

var.Hello

Classes in Ruby are objects and variables can be used to refer to them.
 
J

jacob.dunphy

And with this, you can do the var = Kernel.const_get("ClassName") and
you have a reference to that class for the scope of your operation.
 
A

ara.t.howard

And with this, you can do the var = Kernel.const_get("ClassName") and
you have a reference to that class for the scope of your operation.

imho const_get is just to fragile for most uses, i prefer something
like this:



cfp:~ > cat a.rb
class Class
def self.for string
value =
Thread.new do
$SAFE = 4
eval string.to_s, TOPLEVEL_BINDING.dup
end.value
raise ArgumentError unless value.is_a? Class
value
end
end

p Class.for('File::Stat')
p Class.for('Foo::Bar')



cfp:~ > ruby a.rb
File::Stat
a.rb:6:in `eval': (eval):1: uninitialized constant Foo (NameError)
from a.rb:4:in `value'
from a.rb:4:in `for'
from a.rb:14



a @ http://codeforpeople.com/
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top