From: Joel VanderWerf Date: 2006-01-27T12:16:18+09:00 Subject: Re: Pass References To Methods As Arguments? wegzumir wrote: > Hey all. I am trying to determine the Ruby syntax for passing a reference > to a method. I want to actually pass the reference itself as an argument to > another method. > > Consider the following simplified Perl example. It features two subroutine > functions and another subroutine that is capable of calling any subroutine > passed to it as a reference. > > > sub print_unix_line { > print shift, "\n" > } > > sub print_windows_line { > print shift, "\015\012"; > } > > sub call_a_routine { > my ($routine, $argument) = @_; > $routine->($argument); > } > > &call_a_routine(\&print_unix_line, 'Hello, unix World!'); > &call_a_routine(\&print_windows_line, 'Hello, Windows World!'); > > > The "\&" prefixed parts are references to subroutines. I have tried to > recreate this logic in Ruby using Proc blocks, but I can't get it to work > properly. Any suggestions? > # first, using a proc print_unix_line = lambda { |arg| print arg, "\n" } # second, using a method def print_windows_line_method arg print arg, "\015\012" end print_windows_line = method :print_windows_line_method def call_a_routine routine, arg routine.call(arg) # or routine[arg] end call_a_routine print_unix_line, 'Hello, unix World!' call_a_routine print_windows_line, 'Hello, Windows World!' -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407