Passing functions as arguments

Discussion in 'Ruby' started by Alex Polite, Jan 15, 2006.

  1. Alex Polite

    Alex Polite Guest

    I'm just starting out with ruby.

    I'm trying to make an array of arrays containing [a pattern, a string,
    a function]

    Patterns and strings went fine but when I put in a function ruby tries
    to eval it and complains about not getting right number of arguments.

    Is there any way to escape a method so that you can put it in a list
    or pass it as an argument to another function?

    pats =3D [
    [/:BEGIN:(.*)/, "DTSTART", Df.datestr2time ],
    [/:DUE:(.*)/, "DUE", Df.datestr2time]
    ]


    alex

    --
    Alex Polite
    http://flosspick.org - finding the right open source
     
    Alex Polite, Jan 15, 2006
    #1
    1. Advertising

  2. Alex Polite

    Phil Tomson Guest

    In article <>,
    Alex Polite <> wrote:
    >I'm just starting out with ruby.
    >
    >I'm trying to make an array of arrays containing [a pattern, a string,
    >a function]
    >
    >Patterns and strings went fine but when I put in a function ruby tries
    >to eval it and complains about not getting right number of arguments.
    >
    >Is there any way to escape a method so that you can put it in a list
    >or pass it as an argument to another function?
    >
    >pats =3D [
    > [/:BEGIN:(.*)/, "DTSTART", Df.datestr2time ],
    > [/:DUE:(.*)/, "DUE", Df.datestr2time]
    > ]
    >


    How about:

    pats = [
    [/:BEGIN:(.*)/, "DRSTART", Df.method:)datestr2time)],
    [/:DUE:(.*)/, "DUE", Df.method:)datestr2time)]
    ]



    Phil
     
    Phil Tomson, Jan 15, 2006
    #2
    1. Advertising

  3. Alex Polite

    Guest

    On Mon, 16 Jan 2006, Alex Polite wrote:

    > I'm just starting out with ruby.
    >
    > I'm trying to make an array of arrays containing [a pattern, a string,
    > a function]
    >
    > Patterns and strings went fine but when I put in a function ruby tries
    > to eval it and complains about not getting right number of arguments.
    >
    > Is there any way to escape a method so that you can put it in a list
    > or pass it as an argument to another function?
    >
    > pats = [
    > [/:BEGIN:(.*)/, "DTSTART", Df.datestr2time ],
    > [/:DUE:(.*)/, "DUE", Df.datestr2time]
    > ]


    yes. but you don't need to, simply store the name of the method and use
    'send':

    harp:~ > cat a.rb
    table =
    [%r/:BEGIN:(.*)/, "DTSTART", "datestr2time"],
    [%r/:DUE:(.*)/, "DUE", "datestr2time"]

    #
    # mock-up
    #
    class Df
    def self::datestr2time string
    p string
    end
    end

    txt = <<-txt
    :BEGIN:
    :DUE:
    txt

    txt.each do |line|
    table.each do |dispatch|
    pat, string, method = dispatch
    if pat.match line
    Df::send method, string
    end
    end
    end


    harp:~ > ruby a.rb
    "DTSTART"
    "DUE"


    regards.

    -a
    --
    strong and healthy, who thinks of sickness until it strikes like lightning?
    preoccupied with the world, who thinks of death, until it arrives like
    thunder? -- milarepa
     
    , Jan 16, 2006
    #3
  4. 'send' is _much_ slower than 'method.call'. The first example is
    probably the clearest and most efficient way to do this, especially in
    a high-use function.
     
    Timothy Goddard, Jan 18, 2006
    #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. ILLOGIC
    Replies:
    1
    Views:
    383
    Rob Williscroft
    Jun 1, 2004
  2. Joel
    Replies:
    12
    Views:
    522
  3. Replies:
    7
    Views:
    580
    Alex Hunsley
    Aug 12, 2006
  4. toomanyjoes

    Passing objects as a functions arguments

    toomanyjoes, Aug 21, 2003, in forum: Javascript
    Replies:
    0
    Views:
    90
    toomanyjoes
    Aug 21, 2003
  5. oldyork90
    Replies:
    10
    Views:
    373
    Jorge
    Sep 27, 2008
Loading...

Share This Page