Calling a procedure with dinamyc name

Discussion in 'Ruby' started by Marcelo Paniagua, Jun 4, 2005.

  1. Hi there!

    I'm using Kirbybase database and it has the peculiar way to access
    fields data though methods. Is it posible to call such methods
    passing the fieldname in a dinamic way?

    instead of

    client_id = table.client_id

    I would like to use sometime like

    field = 'client_id'
    client_id = table.field or something like that.

    This is also applicable to a program that I'm developing, in which I
    want to find if a class has a given method... for example, I want to
    find if a class Card has an "onplay" method.

    Thanks

    --
    Este correo esta libre de virus!
    Marcelo Paniagua, Jun 4, 2005
    #1
    1. Advertising

  2. On Jun 4, 2005, at 1:52 PM, Marcelo Paniagua wrote:

    > Hi there!
    >
    > I'm using Kirbybase database and it has the peculiar way to access
    > fields data though methods. Is it posible to call such methods
    > passing the fieldname in a dinamic way?
    >
    > instead of
    >
    > client_id = table.client_id
    >
    > I would like to use sometime like
    >
    > field = 'client_id'
    > client_id = table.field or something like that.
    >
    > This is also applicable to a program that I'm developing, in which I
    > want to find if a class has a given method... for example, I want to
    > find if a class Card has an "onplay" method.
    > Thanks
    >


    Maybe you want this:

    field = 'client_id'
    client_id = table.send field

    or maybe you want this

    method = table.method :client_id
    client_id = method.call

    ... you've got quite a few options.



    > --
    > Este correo esta libre de virus!
    >
    >
    >


    Francis Hwang
    http://fhwang.net/
    Francis Hwang, Jun 4, 2005
    #2
    1. Advertising

  3. For the the Kirbybase it seems like the right option. As for the other
    example I have my doubts. What I want to do in that
    case is something like this: I have an object of class Card. I want to
    look though the object methods and find out if it has
    an "onPlay" method, "onLefPlay" method, and so on. I want to do this
    in order to let the user knows that he can use those
    methods in a given time... Which option do you think is the right option?

    Marcelo



    Francis Hwang escribió:

    >
    > On Jun 4, 2005, at 1:52 PM, Marcelo Paniagua wrote:
    >
    >> Hi there!
    >>
    >> I'm using Kirbybase database and it has the peculiar way to access
    >> fields data though methods. Is it posible to call such methods
    >> passing the fieldname in a dinamic way?
    >>
    >> instead of
    >>
    >> client_id = table.client_id
    >>
    >> I would like to use sometime like
    >>
    >> field = 'client_id'
    >> client_id = table.field or something like that.
    >>
    >> This is also applicable to a program that I'm developing, in which
    >> I want to find if a class has a given method... for example, I want to
    >> find if a class Card has an "onplay" method.
    >> Thanks
    >>

    >
    > Maybe you want this:
    >
    > field = 'client_id'
    > client_id = table.send field
    >
    > or maybe you want this
    >
    > method = table.method :client_id
    > client_id = method.call
    >
    > ... you've got quite a few options.
    >
    >
    >
    >> --
    >> Este correo esta libre de virus!
    >>
    >>
    >>

    >
    > Francis Hwang
    > http://fhwang.net/
    >
    >
    >


    --
    Este correo esta libre de virus!
    Marcelo Paniagua, Jun 4, 2005
    #3
  4. Marcelo Paniagua wrote:
    > Hi there!
    >
    > I'm using Kirbybase database and it has the peculiar way to access
    > fields data though methods. Is it posible to call such methods
    > passing the fieldname in a dinamic way?
    >
    > instead of
    >
    > client_id = table.client_id
    >
    > I would like to use sometime like
    >
    > field = 'client_id'
    > client_id = table.field or something like that.


    There are at least two basic options:

    1. Call Object#send:

    field = 'client_id'
    client_id = table.send(field)

    2. Use Object#method to get a method, then call it:

    field = 'client_id'
    method = table.method(field)
    method.call

    > This is also applicable to a program that I'm developing, in which I
    > want to find if a class has a given method... for example, I want to
    > find if a class Card has an "onplay" method.
    > Thanks


    You can ask an instance of Card if it responds to that method:

    card = Card.new
    if card.respond_to?('onplay')
    # Do something
    end

    You may also want to research the method 'method_missing' which allows
    for a lot of interesting tricks.

    Regards,
    Ryan
    Ryan Leavengood, Jun 4, 2005
    #4
  5. You could look at either Object#methods or Object#respond_to? depending=20=

    on how you want to do it.

    On Jun 4, 2005, at 2:05 PM, Marcelo Paniagua wrote:

    > For the the Kirbybase it seems like the right option. As for the=20
    > other example I have my doubts. What I want to do in that
    > case is something like this: I have an object of class Card. I want=20=


    > to look though the object methods and find out if it has
    > an "onPlay" method, "onLefPlay" method, and so on. I want to do this=20=


    > in order to let the user knows that he can use those
    > methods in a given time... Which option do you think is the right=20
    > option?
    >
    > Marcelo
    >
    >
    >
    > Francis Hwang escribi=F3:
    >
    >>
    >> On Jun 4, 2005, at 1:52 PM, Marcelo Paniagua wrote:
    >>
    >>> Hi there!
    >>>
    >>> I'm using Kirbybase database and it has the peculiar way to access=20=


    >>> fields data though methods. Is it posible to call such methods
    >>> passing the fieldname in a dinamic way?
    >>>
    >>> instead of
    >>>
    >>> client_id =3D table.client_id
    >>>
    >>> I would like to use sometime like
    >>>
    >>> field =3D 'client_id'
    >>> client_id =3D table.field or something like that.
    >>>
    >>> This is also applicable to a program that I'm developing, in which=20=


    >>> I want to find if a class has a given method... for example, I want=20=


    >>> to
    >>> find if a class Card has an "onplay" method.
    >>> Thanks
    >>>

    >>
    >> Maybe you want this:
    >>
    >> field =3D 'client_id'
    >> client_id =3D table.send field
    >>
    >> or maybe you want this
    >>
    >> method =3D table.method :client_id
    >> client_id =3D method.call
    >>
    >> ... you've got quite a few options.
    >>
    >>
    >>
    >>> --=20
    >>> Este correo esta libre de virus!
    >>>
    >>>
    >>>

    >>
    >> Francis Hwang
    >> http://fhwang.net/
    >>
    >>
    >>

    >
    > --=20
    > Este correo esta libre de virus!
    >
    >
    >


    Francis Hwang
    http://fhwang.net/
    Francis Hwang, Jun 4, 2005
    #5
  6. Thanks! I think that for Kirbybase I will get the method using the
    fieldname and for the other case I will use the Object#respond_to?.

    Marcelo Paniagua

    Ryan Leavengood escribió:

    > Marcelo Paniagua wrote:
    >
    >> Hi there!
    >>
    >> I'm using Kirbybase database and it has the peculiar way to access
    >> fields data though methods. Is it posible to call such methods
    >> passing the fieldname in a dinamic way?
    >>
    >> instead of
    >>
    >> client_id = table.client_id
    >>
    >> I would like to use sometime like
    >>
    >> field = 'client_id'
    >> client_id = table.field or something like that.

    >
    >
    > There are at least two basic options:
    >
    > 1. Call Object#send:
    >
    > field = 'client_id'
    > client_id = table.send(field)
    >
    > 2. Use Object#method to get a method, then call it:
    >
    > field = 'client_id'
    > method = table.method(field)
    > method.call
    >
    >> This is also applicable to a program that I'm developing, in which
    >> I want to find if a class has a given method... for example, I want to
    >> find if a class Card has an "onplay" method.
    >> Thanks

    >
    >
    > You can ask an instance of Card if it responds to that method:
    >
    > card = Card.new
    > if card.respond_to?('onplay')
    > # Do something
    > end
    >
    > You may also want to research the method 'method_missing' which allows
    > for a lot of interesting tricks.
    >
    > Regards,
    > Ryan
    >
    >


    --
    Este correo esta libre de virus!
    Marcelo Paniagua, Jun 4, 2005
    #6
    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?B?bW9vcJk=?=
    Replies:
    7
    Views:
    810
    Roedy Green
    Jan 2, 2006
  2. ding feng
    Replies:
    2
    Views:
    2,790
    ding feng
    Jun 25, 2003
  3. Mike P
    Replies:
    0
    Views:
    3,280
    Mike P
    Jun 19, 2006
  4. Bobby Chamness
    Replies:
    2
    Views:
    2,380
    Joe Smith
    Apr 22, 2007
  5. AlexWare
    Replies:
    2
    Views:
    746
    Paul Uiterlinden
    Oct 23, 2009
Loading...

Share This Page