function call using reflection

Discussion in 'Ruby' started by Rajarshi Chakravarty, Aug 20, 2010.

  1. Hi,
    I get each part of the following command in a string at runtime
    Mod1::Util.parser1("log.txt")

    So I have the following strings:
    s1 = "Mod1"
    s2 = "Util"
    s3 = "parser1"
    s4 = "log.txt"

    Now I want to prepare the command and execute it at runtime.

    Object::const_get(s1 + "::" + s2 + "." + s3 + "(\"" + s4 + "\")")
    does not work.

    Please help..
    --
    Posted via http://www.ruby-forum.com/.
     
    Rajarshi Chakravarty, Aug 20, 2010
    #1
    1. Advertising

  2. 2010/8/20 Rajarshi Chakravarty <>:
    > Hi,
    > I get each part of the following command in a string at runtime
    > Mod1::Util.parser1("log.txt")
    >
    > So I have the following strings:
    > s1 = "Mod1"
    > s2 = "Util"
    > s3 = "parser1"
    > s4 = "log.txt"
    >
    > Now I want to prepare the command and execute it at runtime.
    >
    > Object::const_get(s1 + "::" + s2 + "." + s3 + "(\"" + s4 + "\")")
    > does not work.


    const_get will only resolve a constant (as the name indicates). You
    could either use eval instead but I recommend the more robust and
    secure version with const_get and send.

    Object::const_get(s1).const_get(s2).send(s3, s4)

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Aug 20, 2010
    #2
    1. Advertising

  3. On Fri, Aug 20, 2010 at 1:12 PM, Rajarshi Chakravarty
    <> wrote:
    > Hi,
    > I get each part of the following command in a string at runtime
    > Mod1::Util.parser1("log.txt")
    >
    > So I have the following strings:
    > s1 = "Mod1"
    > s2 = "Util"
    > s3 = "parser1"
    > s4 = "log.txt"
    >
    > Now I want to prepare the command and execute it at runtime.
    >
    > Object::const_get(s1 + "::" + s2 + "." + s3 + "(\"" + s4 + "\")")
    > does not work.


    If you know the first one is a module and the second a class, you could try:

    irb(main):001:0> module A
    irb(main):002:1> class B
    irb(main):003:2> def parse(param)
    irb(main):004:3> puts "parsing #{param} ..."
    irb(main):005:3> end
    irb(main):006:2> end
    irb(main):007:1> end
    => nil
    irb(main):008:0> s1 = "A"
    => "A"
    irb(main):009:0> s2 = "B"
    => "B"
    irb(main):010:0> s3 = "parse"
    => "parse"
    irb(main):011:0> s4 = "filename.txt"
    => "filename.txt"
    irb(main):014:0> m = Object.const_get(s1)
    => A
    irb(main):015:0> c = m.const_get(s2)
    => A::B
    irb(main):017:0> c.new.send(s3, s4)
    parsing filename.txt ...

    Jesus.
     
    Jesús Gabriel y Galán, Aug 20, 2010
    #3
  4. Rajarshi Chakravarty, Aug 20, 2010
    #4
    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. Soefara
    Replies:
    2
    Views:
    6,377
    pete kirkham
    Aug 20, 2003
  2. grbgooglefan
    Replies:
    2
    Views:
    453
    Pascal Bourguignon
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    4
    Views:
    471
    Kenny McCormack
    Jan 30, 2008
  4. grbgooglefan
    Replies:
    0
    Views:
    423
    grbgooglefan
    Jan 30, 2008
  5. Alok
    Replies:
    3
    Views:
    286
Loading...

Share This Page