passing function into another function

Discussion in 'Ruby' started by Joe Van Dyk, Sep 12, 2006.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    Hi,

    I couldn't work out the syntax for the following:

    class Reports
    def self.This; end
    def self.That; end
    end

    def do_something_with_a_report report
    # calls Reports::This or Reports::That, dtermined by what report is
    # something like report.call ?
    end

    I want to do something like:

    do_something_with_a_report Reports::This

    But that calls Reports::This and sends that value to the
    do_something_with_a_report function.

    In other words, I want to do something like passing a function pointer
    to a function in C.
    Joe Van Dyk, Sep 12, 2006
    #1
    1. Advertising

  2. Joe Van Dyk

    Guest

    On Tue, 12 Sep 2006, Joe Van Dyk wrote:

    > Hi,
    >
    > I couldn't work out the syntax for the following:
    >
    > class Reports
    > def self.This; end
    > def self.That; end
    > end
    >
    > def do_something_with_a_report report
    > # calls Reports::This or Reports::That, dtermined by what report is
    > # something like report.call ?
    > end
    >
    > I want to do something like:
    >
    > do_something_with_a_report Reports::This
    >
    > But that calls Reports::This and sends that value to the
    > do_something_with_a_report function.
    >
    > In other words, I want to do something like passing a function pointer
    > to a function in C.
    >


    harp:~ > cat a.rb
    class Reports
    def self.This; p 'This'; end
    def self.That; p 'That'; end
    This = method 'This'
    That = method 'That'
    end

    def do_something_with_a_report report
    report.call
    end

    do_something_with_a_report Reports::This
    do_something_with_a_report Reports::That


    harp:~ > ruby a.rb
    "This"
    "That"

    you could also just

    do_something_with_a_report Reports::method('This')
    do_something_with_a_report Reports::method('That')


    -a
    --
    in order to be effective truth must penetrate like an arrow - and that is
    likely to hurt. -- wei wu wei
    , Sep 12, 2006
    #2
    1. Advertising

  3. Joe Van Dyk

    Joe Van Dyk Guest

    On 9/11/06, <> wrote:
    > On Tue, 12 Sep 2006, Joe Van Dyk wrote:
    >
    > > Hi,
    > >
    > > I couldn't work out the syntax for the following:
    > >
    > > class Reports
    > > def self.This; end
    > > def self.That; end
    > > end
    > >
    > > def do_something_with_a_report report
    > > # calls Reports::This or Reports::That, dtermined by what report is
    > > # something like report.call ?
    > > end
    > >
    > > I want to do something like:
    > >
    > > do_something_with_a_report Reports::This
    > >
    > > But that calls Reports::This and sends that value to the
    > > do_something_with_a_report function.
    > >
    > > In other words, I want to do something like passing a function pointer
    > > to a function in C.
    > >

    >
    > harp:~ > cat a.rb
    > class Reports
    > def self.This; p 'This'; end
    > def self.That; p 'That'; end
    > This = method 'This'
    > That = method 'That'
    > end
    >
    > def do_something_with_a_report report
    > report.call
    > end
    >
    > do_something_with_a_report Reports::This
    > do_something_with_a_report Reports::That
    >
    >
    > harp:~ > ruby a.rb
    > "This"
    > "That"
    >
    > you could also just
    >
    > do_something_with_a_report Reports::method('This')
    > do_something_with_a_report Reports::method('That')


    Ah, Object#method. Thank you both!

    Joe
    Joe Van Dyk, Sep 12, 2006
    #3
    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. Newsgroup - Ann
    Replies:
    5
    Views:
    600
    John Carson
    Jul 30, 2003
  2. someone
    Replies:
    26
    Views:
    865
    Jorgen Grahn
    Oct 24, 2011
  3. Renie83
    Replies:
    2
    Views:
    232
  4. Robert Cohen
    Replies:
    3
    Views:
    265
    Andrew Durstewitz
    Jul 15, 2003
  5. Ken Soon

    Passing variables into another script

    Ken Soon, May 21, 2007, in forum: Perl Misc
    Replies:
    15
    Views:
    200
    Tad McClellan
    May 23, 2007
Loading...

Share This Page