How to Call a static method?

  • Thread starter Daniel Völkerts
  • Start date
D

Daniel Völkerts

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...
 
L

Logan Capaldo

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...
Just do it
if condition
Role.something
else
Organisation.anotherthing
end

Nothing special.
 
S

Simen Edvardsen

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 cal= l

Role.something or

Organisation.anotherthing

depening which parameter was passed.

Please give a little hint to proceed. Thanks a lot...

class Test
def self.test
puts "The huge test...OF DOOM!"
end
end
Test.test # =3D> 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

--=20
- Simen
 
D

Daniel Völkerts

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,
 
L

Logan Capaldo

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. send

greetings,
 
J

Jan Svitok

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.

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.
 
D

Daniel Völkerts

Jan said:
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!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top