From: Dave Burt Date: 2005-07-31T17:51:03+09:00 Subject: Re: Higher Order Functions "Nickolay Kolev" wrote... > ... > module Functional > > def Functional.every(f, lst) > lst.collect { |element| f(element) } > end > > end > > in test.rb: > > require 'fruby' > > def times_two(x) > x * 2 > end > > p Functional.every(:times_two, [1,2,3]) > > > And I get this: > > undefined method `f' for Functional:Module (NoMethodError) The problem here is that f(element) means call the method self.f (which doesn't exist, hence the error), whereas you want to call the "function" represented by the parameter f. You can do this a couple of ways. Charles suggested you pass f as a Method to every: > p Functional.every(method(:times_two), [1,2,3] > And in Functional, it would be lst.collect { |element| f.call(element) > } f.call will also work if f is a Proc, so you could also do: Functional.every(proc{|x| x * 2}, [1, 2, 3]) You can pass a symbol, but I wouldn't recommend it - make the caller pass a callable object (a Proc or Method). Cheers, Dave