[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Pass References To Methods As Arguments?

wegzumir

1/27/2006 1:42:00 AM

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?
6 Answers

konsu

1/27/2006 3:01:00 AM

0

Hello,

would Object#method work?

konstantin

"wegzumir" <wegzumir@gmail.com> wrote in message
news:be27a0f30601261741h33095cebv75645cbca091a97@mail.gmail.com...
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?


Austin Ziegler

1/27/2006 3:14:00 AM

0

On 26/01/06, wegzumir <wegzumir@gmail.com> 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.

There is no way to do that directly in Ruby.

> 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!');

Your example really isn't a good one, but:

1.

print_unix_line = lambda { |arg| print arg + "\n" }
print_wind_line = lambda { |arg| print arg + "\015\012" }

def call_a_routine(routine, argument)
routine[argument]
# or: routine.call(argument)
end

call_a_routine(print_unix_line, "Hello, unix.")
call_a_routine(print_wind_line, "Hello, Windows.")

2.

class Routines
def print_unix_line(arg)
print arg + "\n"
end

def print_wind_line(arg)
print arg + "\015\012"
end
end

def call_a_routine(routine, argument)
@@routine ||= Routines.new
@@routine.__send__(routine, argument)
end

call_a_routine(:print_unix_line, "Hello, unix.")
call_a_routine(:print_wind_line, "Hello, Windows.")

As far as the poorness of the example you have, you don't need to do
anything special with Ruby to print properly:

puts "Hello Ruby, World"

What exactly are you really trying to do?

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Joel VanderWerf

1/27/2006 3:16:00 AM

0

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


wegzumir

1/27/2006 4:28:00 AM

0

On 1/26/06, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
>
> # 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
>
>

Thanks for the reply! One of the things I had tried was using the
colon character like that, but I didn't know about also passing the
resulting symbol to "method" as an argument like that. And of course I
also wasn't going down the lamdba path which I'll have to do more
experimenting with.

Much appreciated.


wegzumir

1/27/2006 4:30:00 AM

0

On 1/26/06, Eric Hodel <drbrain@segment7.net> wrote:
> On Jan 26, 2006, at 5:41 PM, 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"
> > }
>
> def print_unix(line)
> print "#{line}\n"
> end
>
> > sub print_windows_line {
> > print shift, "\015\012";
> > }
>
> def print_windows(line)
> print "#{line}\r\n"
> end
>
> > sub call_a_routine {
> > my ($routine, $argument) = @_;
> > $routine->($argument);
> > }
>
> def call_a_routine(args)
> yield args
> end
>
> > &call_a_routine(\&print_unix_line, 'Hello, unix World!');
> > &call_a_routine(\&print_windows_line, 'Hello, Windows World!');
>
> call_a_routine('hello, unix world!') { |line| print_unix line }
> call_a_routine('hello, windows world') { |line| print_windows line }
>
> > 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?
>
> You can turn a Method into a block argument with &:
>
> call_a_routine 'text', &method(:print_unix)
> call_a_routine 'text', &method(:print_windows)
>
> but don't do that, its ugly. Use blocks.
>
> --
> Eric Hodel - drbrain@segment7.net - http://se...
> This implementation is HODEL-HASH-9600 compliant
>
> http://trackmap.rob...
>
>
>
>

Cool, so there is a "&" prefix that's available for use like that. But
I agree with you on it not being as readable in the end. I will have
to work on my block fu a bit.

Many thanks for replying!


wegzumir

1/27/2006 4:39:00 AM

0

On 1/26/06, Austin Ziegler <halostatue@gmail.com> wrote:
> On 26/01/06, wegzumir <wegzumir@gmail.com> 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.
>
> There is no way to do that directly in Ruby.
>
> > 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!');
>
> Your example really isn't a good one, but:
>
> 1.
>
> print_unix_line = lambda { |arg| print arg + "\n" }
> print_wind_line = lambda { |arg| print arg + "\015\012" }
>
> def call_a_routine(routine, argument)
> routine[argument]
> # or: routine.call(argument)
> end
>
> call_a_routine(print_unix_line, "Hello, unix.")
> call_a_routine(print_wind_line, "Hello, Windows.")
>
> 2.
>
> class Routines
> def print_unix_line(arg)
> print arg + "\n"
> end
>
> def print_wind_line(arg)
> print arg + "\015\012"
> end
> end
>
> def call_a_routine(routine, argument)
> @@routine ||= Routines.new
> @@routine.__send__(routine, argument)
> end
>
> call_a_routine(:print_unix_line, "Hello, unix.")
> call_a_routine(:print_wind_line, "Hello, Windows.")
>
> As far as the poorness of the example you have, you don't need to do
> anything special with Ruby to print properly:
>
> puts "Hello Ruby, World"
>
> What exactly are you really trying to do?
>
> -austin
> --
> Austin Ziegler * halostatue@gmail.com
> * Alternate: austin@halostatue.ca
>
>

Yes, my apologies for the example! I was actually trying to strip down
the problem to its barest essentials by making up a hypothetical
scenario.

The whole reason for this (the non hypothetical reason) is because of
the excellent computer science concepts expressed in the book
"Higher-Order Perl". I've used some of those concepts in my Perl code
in the past and I wanted to be able to express the same ideas in Ruby
(and of course be completely open to learning the more Ruby way of
doing the same things instead). In particular, one of the examples at
the beginning of the book is a recursive directory traverser that
accepts references to subroutines for arguments that are mapped as
handlers for what to do when a directory is first opened, when each
file is processed, and once the directory has been finished with.

Many thanks for the two solutions!