Forum: Ruby Passing functions as arguments

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Alex P. (Guest)
on 2006-01-15 23:41
(Received via mailing list)
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]

Farrel L. (Guest)
on 2006-01-16 00:09
(Received via mailing list)
formatmethod = Df.method(:datestr2time)
unknown (Guest)
on 2006-01-16 02:54
(Received via mailing list)
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

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

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

   txt = <<-txt

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

   harp:~ > ruby a.rb


This topic is locked and can not be replied to.