Idiom -- is there a better way?

Discussion in 'Ruby' started by Mark Probert, Oct 20, 2004.

  1. Mark Probert

    Mark Probert Guest

    Hi ..

    I am doing indirect dispatch, something like:

    class Foo
    def bar(fn)
    eval "self.#{fn}"
    end
    end

    class Bar < Foo
    def blah
    puts "blah here"
    end
    end

    f = Bar.new
    f.bar("blah")

    It works however I am sure there must be a nicer way of doing the bar(fn)
    stuff. Any ideas?

    TIA,
    -mark.
    Mark Probert, Oct 20, 2004
    #1
    1. Advertising

  2. Mark Probert

    Kevin Howe Guest

    > class Foo
    > def bar(fn)
    > eval "self.#{fn}"
    > end
    > end
    >
    > ...nicer way of doing the bar(fn) stuff. Any ideas?
    >


    Replace eval "self.#{fn}" with method(fn).call
    Kevin Howe, Oct 20, 2004
    #2
    1. Advertising

  3. On Wed, 2004-10-20 at 19:54, Mark Probert wrote:
    > Hi ..
    >
    > I am doing indirect dispatch, something like:
    > class Foo
    > def bar(fn)
    > eval "self.#{fn}"
    > end
    > end


    Try self.send(fn) instead.
    Alexey Verkhovsky, Oct 20, 2004
    #3
  4. On Wednesday 20 October 2004 12:54 pm, Mark Probert wrote:
    | Hi ..
    |
    | I am doing indirect dispatch, something like:
    |
    | class Foo
    | def bar(fn)
    self.send(fn)
    | end
    | end
    |
    | class Bar < Foo
    | def blah
    | puts "blah here"
    | end
    | end
    |
    | f = Bar.new
    | f.bar("blah")
    |
    | It works however I am sure there must be a nicer way of doing the bar(fn)
    | stuff. Any ideas?
    |
    | TIA,
    | -mark.

    --
    ( o _ カラãƒ
    // trans.
    / \

    I don't give a damn for a man that can only spell a word one way.
    -Mark Twain
    trans. (T. Onoma), Oct 20, 2004
    #4
  5. On Oct 20, 2004, at 10:54 AM, Mark Probert wrote:
    > class Foo
    > def bar(fn)
    > eval "self.#{fn}"
    > end
    > end
    >
    > class Bar < Foo
    > def blah
    > puts "blah here"
    > end
    > end
    >
    > f = Bar.new
    > f.bar("blah")


    Does the function need to be a string argument?

    f.bar( f.method( :blah ) ) # Pass the actual method reference

    or, as others have noted:

    class Foo
    def bar( meth_name )
    self.method(m).call
    end
    end
    ...
    f.bar( :blah )
    Gavin Kistner, Oct 21, 2004
    #5
  6. "Mark Probert" <> schrieb im Newsbeitrag
    news:Xns9588647C9B7C4probertmnospamacmorg@198.161.157.145...
    >
    > Hi ..
    >
    > I am doing indirect dispatch, something like:
    >
    > class Foo
    > def bar(fn)
    > eval "self.#{fn}"
    > end
    > end
    >
    > class Bar < Foo
    > def blah
    > puts "blah here"
    > end
    > end
    >
    > f = Bar.new
    > f.bar("blah")
    >
    > It works however I am sure there must be a nicer way of doing the

    bar(fn)
    > stuff. Any ideas?
    >
    > TIA,
    > -mark.


    There's no need for method bar because that is built into Ruby alread:
    Object#send does the job:

    class Bar
    def blah
    puts "blah here"
    end
    end


    >> Bar.new.send :blah

    blah here
    => nil
    >> Bar.new.send "blah"

    blah here
    => nil
    >>


    Kind regards

    robert
    Robert Klemme, Oct 21, 2004
    #6
  7. Mark Probert

    Mark Probert Guest

    Hi ..

    Gavin Kistner <> wrote:

    > On Oct 20, 2004, at 10:54 AM, Mark Probert wrote:
    >> class Foo
    >> def bar(fn)
    >> eval "self.#{fn}"
    >> end
    >> end

    >
    > Does the function need to be a string argument?
    >


    Yup. What I am doing is taking a YAML entry and converting it into a
    method call for a derived class.

    Thanks to all for the help.


    --
    -mark. (probertm @ acm dot org)
    Mark Probert, Oct 21, 2004
    #7
  8. Mark Probert

    John Carter Guest

    #On Thu, 21 Oct 2004, Mark Probert wrote:
    #
    #Try

    class Foo
    def bar(fn)
    send(fn)
    end
    end

    class Bar < Foo
    def blah
    puts "blah here"
    end
    end

    f = Bar.new
    f.bar:)blah)




    #
    #John Carter Phone : (64)(3) 358 6639
    #Tait Electronics Fax : (64)(3) 359 4632
    #PO Box 1645 Christchurch Email :
    #New Zealand
    #
    # ruby -wc certifies this email "Syntax OK"
    John Carter, Oct 21, 2004
    #8
    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. Peter Bencsik
    Replies:
    2
    Views:
    804
  2. MooMaster
    Replies:
    11
    Views:
    413
    MooMaster
    Apr 9, 2009
  3. Paul Rubin
    Replies:
    5
    Views:
    404
    Hendrik van Rooyen
    Aug 6, 2009
  4. slothbear
    Replies:
    3
    Views:
    120
    slothbear
    Dec 4, 2007
  5. Replies:
    2
    Views:
    39
    Mark H Harris
    May 13, 2014
Loading...

Share This Page