How to Call a static method?

Discussion in 'Ruby' started by Daniel Völkerts, Oct 3, 2006.

  1. 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...
    --
    Daniel Völkerts
    Protected by Anti Pesto. -- Wallace & Gromit
    Daniel Völkerts, Oct 3, 2006
    #1
    1. Advertising

  2. On Tue, Oct 03, 2006 at 11:05:43PM +0900, Daniel V?lkerts wrote:
    > 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.
    Logan Capaldo, Oct 3, 2006
    #2
    1. Advertising

  3. On 10/3/06, Daniel V=F6lkerts <> wrote:
    > 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...
    > --
    > Daniel V=F6lkerts
    > Protected by Anti Pesto. -- Wallace & Gromit
    >
    >


    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
    Simen Edvardsen, Oct 3, 2006
    #3
  4. 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,
    --
    Daniel Völkerts
    Protected by Anti Pesto. -- Wallace & Gromit
    Daniel Völkerts, Oct 3, 2006
    #4
  5. On Tue, Oct 03, 2006 at 11:19:55PM +0900, Daniel V?lkerts wrote:
    > 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,
    > --
    > Daniel V?lkerts
    > Protected by Anti Pesto. -- Wallace & Gromit
    Logan Capaldo, Oct 3, 2006
    #5
  6. Daniel Völkerts

    Jan Svitok Guest

    On 10/3/06, Daniel V=F6lkerts <> wrote:
    > 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.
    Jan Svitok, Oct 3, 2006
    #6
  7. Jan Svitok 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!


    --
    Daniel Völkerts
    Protected by Anti Pesto. -- Wallace & Gromit
    Daniel Völkerts, Oct 3, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Replies:
    9
    Views:
    11,183
    Jezuch
    Jul 3, 2003
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    Why do static and non-static method names collide?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 2, 2003, in forum: Java
    Replies:
    12
    Views:
    5,584
    cgbusch
    Jul 5, 2003
  3. Gary
    Replies:
    1
    Views:
    3,950
    Ryan Stewart
    Jan 16, 2004
  4. Michael
    Replies:
    3
    Views:
    4,911
    Adam Jenkins
    Jan 27, 2004
  5. Replies:
    2
    Views:
    497
    Jonathan Mcdougall
    Aug 6, 2007
Loading...

Share This Page