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?