# Passing functions as arguments

**URL:** <https://www.ruby-forum.com/t/passing-functions-as-arguments/52531>\
**Category:** Ruby\
**Created:** [January 15, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/passing-functions-as-arguments/52531 "2006-01-15T22:41:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex789](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alex789](https://www.ruby-forum.com/u/alex789)\
**Post date:** [January 15, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/passing-functions-as-arguments/52531/1 "2006-01-15T22:41:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alex789](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alex789](https://www.ruby-forum.com/u/alex789)\
**Post date:** [January 15, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/passing-functions-as-arguments/52531/2 "2006-01-15T23:09:00Z")

</div>

formatmethod = Df.method(:datestr2time)

---

<div class="post-metadata">

**Author:** ![alex789](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bc8723/32.png) [@alex789](https://www.ruby-forum.com/u/alex789)\
**Post date:** [January 16, 2006, 1:54am UTC](https://www.ruby-forum.com/t/passing-functions-as-arguments/52531/3 "2006-01-16T01:54:38Z")

</div>

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
