Passing functions as arguments


#1

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]
]

alex


#2

formatmethod = Df.method(:datestr2time)


#3

On Mon, 16 Jan 2006, Alex P. wrote:

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