[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Calling a Class Method with a class name

Torsten Robitzki

6/8/2006 7:52:00 PM

Hi,
I'm looking for a way to call a Class Method with the class name being a
variable string and the method name is fixed. Something like :
s = "ClassName"
eval ("#{s}.name")

But unfortunately I have to call two such functions in chain and both
have a block as parameter, so eval would be my last choice. So I'm
looking for a way to call the function like I could call them if it
would be an Object Method:
Class.get_objet_by_name("ClassName").send(:name)

any suggestions?

best regards

7 Answers

Premshree Pillai

6/8/2006 8:15:00 PM

0

Torsten Robitzki wrote:
> But unfortunately I have to call two such functions in chain and both
> have a block as parameter, so eval would be my last choice. So I'm
> looking for a way to call the function like I could call them if it
> would be an Object Method:
> Class.get_objet_by_name("ClassName").send(:name)

Not sure if I understood correctly, but anyway...

str = String.new("ruby")
obj_name = "str"
eval(obj_name).send("length")
=> 4

Would something like this work?

Premshree

Torsten Robitzki

6/8/2006 8:38:00 PM

0

premshree.pillai@gmail.com wrote:

Hi Premshree,

> Not sure if I understood correctly, but anyway...

For sure my fault ;-)

> str = String.new("ruby")
> obj_name = "str"
> eval(obj_name).send("length")
> => 4
>
> Would something like this work?

Jep, that's exactly what I was looking for. I wondered what Type the
expression /eval("str")/ would have and instead of asking here I
thought: "Hey that's ruby why not asking the result itself" =>
So :
puts eval("Test").inspect
Test

So am I'm right that eval("Classname") returns some kind of "default
initialized" Object of type Classname?

best regards and thanks,
Torsten

Daniel Schierbeck

6/8/2006 9:23:00 PM

0

Torsten Robitzki wrote:
> Hi,
> I'm looking for a way to call a Class Method with the class name being a
> variable string and the method name is fixed. Something like :
> s = "ClassName"
> eval ("#{s}.name")
>
> But unfortunately I have to call two such functions in chain and both
> have a block as parameter, so eval would be my last choice. So I'm
> looking for a way to call the function like I could call them if it
> would be an Object Method:
> Class.get_objet_by_name("ClassName").send(:name)

Object.const_get("ClassName").foo


Cheers,
Daniel

Torsten Robitzki

6/9/2006 2:17:00 PM

0

Hi Daniel,

Daniel Schierbeck wrote:
> Object.const_get("ClassName").foo

ri const_get
------------------------------------------------------- Module#const_get
mod.const_get(sym) => obj
------------------------------------------------------------------------
Returns the value of the named constant in _mod_.

Math.const_get(:PI) #=> 3.14159265358979

Is a class name treated like a constant in ruyb? Or is my documentation
not up to date? Or is it just an undocumented feature?

Thanks,
Torsten

Daniel Schierbeck

6/9/2006 4:15:00 PM

0

Torsten Robitzki wrote:
> Is a class name treated like a constant in ruyb? Or is my documentation
> not up to date? Or is it just an undocumented feature?

Both classes and modules are constants -- this is valid:

Foo = Class.new{}
Bar = Module.new{}

If you want to make sure the object returned by Module#const_get is a
class, you can just test its class:

class Module
def class_get(name)
cl = const_get(name)
unless cl.kind_of? Class
raise TypeError, "`#{cl.inspect}' must be a Class"
end
return cl
end
end

of course, you could also just accept all objects that respond to #new,
which would be quackier:

class Module
def class_get(name)
cl = const_get(name)
unless cl.respond_to? :new
raise TypeError, "`#{cl.inspect}' must respond to `new'"
end
return cl
end
end


Cheers,
Daniel

Torsten Robitzki

6/9/2006 9:33:00 PM

0

Daniel Schierbeck wrote:

> Torsten Robitzki wrote:
>
>> Is a class name treated like a constant in ruyb? Or is my
>> documentation not up to date? Or is it just an undocumented feature?
>
>
> Both classes and modules are constants -- this is valid:
>
> Foo = Class.new{}
> Bar = Module.new{}
> ...

Thank you for your detailed and very helpful reply. Tomorrow I will have
to travel by train, so that's a good opportunity to read my "Programming
Ruby" book ;-)

best regards
Torsten

Daniel Schierbeck

6/9/2006 11:52:00 PM

0

Torsten Robitzki wrote:
> Thank you for your detailed and very helpful reply.

No problem at all :)


Daniel