Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Passing functions as arguments

Reply
Thread Tools

Passing functions as arguments

 
 
Alex Polite
Guest
Posts: n/a
 
      01-15-2006
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 ],
[/UE.*)/, "DUE", Df.datestr2time]
]


alex

--
Alex Polite
http://flosspick.org - finding the right open source


 
Reply With Quote
 
 
 
 
Phil Tomson
Guest
Posts: n/a
 
      01-15-2006
In article <ad9751290601151338k1384454fj64b900831961a4f6@mail .gmail.com>,
Alex Polite <(E-Mail Removed)> 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 ],
> [/UE.*)/, "DUE", Df.datestr2time]
> ]
>


How about:

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



Phil
 
Reply With Quote
 
 
 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      01-16-2006
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 ],
> [/UE.*)/, "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/UE.*)/, "DUE", "datestr2time"]

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

txt = <<-txt
:BEGIN:
UE:
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



 
Reply With Quote
 
Timothy Goddard
Guest
Posts: n/a
 
      01-18-2006
'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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
functions and arguments.length; passing unknown number of arguments oldyork90 Javascript 10 09-27-2008 03:05 AM
Passing Classes as Arguments to Functions of Other Classes crouse@physics.wm.edu Java 7 08-12-2006 02:08 AM
passing arguments dynamically to functions Joel C++ 12 07-02-2004 01:21 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Passing objects as a functions arguments toomanyjoes Javascript 0 08-21-2003 08:41 PM



Advertisments