Hi there, after I searched the ruby-doc.org site I couldn’t find a hint
how to call a class method on different classes.
I’ve a helper class which parses a text input and which should either
call
Role.something or
Organisation.anotherthing
depening which parameter was passed.
Please give a little hint to proceed. Thanks a lot…
On Tue, Oct 03, 2006 at 11:05:43PM +0900, Daniel V?lkerts wrote:
Please give a little hint to proceed. Thanks a lot…
Just do it
if condition
Role.something
else
Organisation.anotherthing
end
Nothing special.
On 10/3/06, Daniel Völkerts [email protected] wrote:
Please give a little hint to proceed. Thanks a lot…
Daniel Völkerts
Protected by Anti Pesto. – Wallace & Gromit
class Test
def self.test
puts “The huge test…OF DOOM!”
end
end
Test.test # => The huge test…OF DOOM!
Test.test isn’t really a static method, but a singleton method on
Test. Then just do a test in your method:
def call_one_or_the_other(x)
case x
when SomeClass
Role.something
when OtherClass
Organisation.anotherthing
end
end
On 10/3/06, Daniel Völkerts [email protected] wrote:
Where call is the Ruby function I’m looking for.
Class.send(:method, *options) or
const_get(‘Class’).send(:method, *options)
possibly
const_get(‘Class’).send(:method, *options, &block) but i’m not sure
with this one.
On Tue, Oct 03, 2006 at 11:19:55PM +0900, Daniel V?lkerts wrote:
Where call is the Ruby function I’m looking for.
send
Okay, misleading question.
What if I like to call the classes dynamically e.g.
def mymethod(class,method)
call(class,method,options)
end
?
Where call is the Ruby function I’m looking for.
greetings,
On 10/3/06, Daniel Völkerts [email protected] wrote:
Okay, misleading question.
What if I like to call the classes dynamically e.g.
def mymethod(class,method)
call(class,method,options)
end
Both as strings or symbols?
def mymethod(class,method)
const_get(class).send(method)
end
Jan S. schrieb:
Where call is the Ruby function I’m looking for.
Class.send(:method, *options) or
const_get(‘Class’).send(:method, *options)
possibly
const_get(‘Class’).send(:method, *options, &block) but i’m not sure
with this one.
Great const_get(‘Classname’).send is what I’m looking for. TIA!