From: Kero van Gelder Date: 2001-09-01T03:21:25+09:00 Subject: [ruby-talk:20637] Re: Passing procedures > I am building this class that takes a user defined procedure and then > applies it. The test code looks like this: > > class TestClass > def initialize(&process) > @process = process > end > > def run() > if @process == nil then > puts "No process given" > else > @process.call(self, "this", "that") > end > end > end > > def testfunc(a, b) > puts "1 -> #{a} 2 -> #{b}" > end > > fred = TestClass.new(testfunc) > fred.run > > I am expecting the message "1 -> this 2 -> that", what I get is: > > test.rb:19:in `testfunc': wrong # of arguments(0 for 2) (ArgumentError) > from test.rb:19 > > The problem is the fred = TestClass.new(testfunc) line. It will work if I > put in some parameters but I want the parameter values set when it is called > inside the class, if I declare them on line 19 then I only get those values > back. you are confusing methods and proc: testproc = proc { |a, b| puts "1 -> #{a} 2 -> #{b}" } fred = TestClass.new(&testproc) and because it is no method, @process.call("this", "that") is enough. +--- Kero ------------------------------ kero@chello.nl ---+ | Don't split your mentality without thinking twice | | Proud like a God -- Guano Apes | +--- M38c ------- http://members.chello.nl/~k.vangelder ---+