# Pass References To Methods As Arguments?

**URL:** <https://www.ruby-forum.com/t/pass-references-to-methods-as-arguments/53165>\
**Category:** Ruby\
**Created:** [January 27, 2006, 2:43am UTC](https://www.ruby-forum.com/t/pass-references-to-methods-as-arguments/53165 "2006-01-27T02:43:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wegzumir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e79b87/32.png) [@wegzumir](https://www.ruby-forum.com/u/wegzumir)\
**Post date:** [January 27, 2006, 2:43am UTC](https://www.ruby-forum.com/t/pass-references-to-methods-as-arguments/53165/1 "2006-01-27T02:43:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![wegzumir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e79b87/32.png) [@wegzumir](https://www.ruby-forum.com/u/wegzumir)\
**Post date:** [January 27, 2006, 4:16am UTC](https://www.ruby-forum.com/t/pass-references-to-methods-as-arguments/53165/2 "2006-01-27T04:16:31Z")

</div>

On 26/01/06, wegzumir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

> }
> 
> 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.”)

1. 

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

---

<div class="post-metadata">

**Author:** ![wegzumir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e79b87/32.png) [@wegzumir](https://www.ruby-forum.com/u/wegzumir)\
**Post date:** [January 27, 2006, 4:16am UTC](https://www.ruby-forum.com/t/pass-references-to-methods-as-arguments/53165/3 "2006-01-27T04:16:31Z")

</div>

On Jan 26, 2006, at 5:41 PM, wegzumir wrote:

> 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 H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![wegzumir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e79b87/32.png) [@wegzumir](https://www.ruby-forum.com/u/wegzumir)\
**Post date:** [January 27, 2006, 5:28am UTC](https://www.ruby-forum.com/t/pass-references-to-methods-as-arguments/53165/4 "2006-01-27T05:28:49Z")

</div>

On 1/26/06, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> 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.

---

<div class="post-metadata">

**Author:** ![wegzumir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e79b87/32.png) [@wegzumir](https://www.ruby-forum.com/u/wegzumir)\
**Post date:** [January 27, 2006, 4:16am UTC](https://www.ruby-forum.com/t/pass-references-to-methods-as-arguments/53165/5 "2006-01-27T04:16:31Z")

</div>

wegzumir wrote:

> ```
> print shift, "\n"
> 
> ```
> 
> &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!’

---

<div class="post-metadata">

**Author:** ![wegzumir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e79b87/32.png) [@wegzumir](https://www.ruby-forum.com/u/wegzumir)\
**Post date:** [January 27, 2006, 5:31am UTC](https://www.ruby-forum.com/t/pass-references-to-methods-as-arguments/53165/6 "2006-01-27T05:31:50Z")

</div>

On 1/26/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > functions and another subroutine that is capable of calling any  
> > end  
> > my ($routine, $argument) = @\_;  
> > call\_a\_routine(‘hello, unix world!’) { |line| print\_unix line }  
> > call\_a\_routine ‘text’, &method(:print\_unix)

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!

---

<div class="post-metadata">

**Author:** ![wegzumir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e79b87/32.png) [@wegzumir](https://www.ruby-forum.com/u/wegzumir)\
**Post date:** [January 27, 2006, 5:40am UTC](https://www.ruby-forum.com/t/pass-references-to-methods-as-arguments/53165/7 "2006-01-27T05:40:57Z")

</div>

On 1/26/06, Austin Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > $routine->($argument);
> > 
> > ```
> 
> print\_wind\_line = lambda { |arg| print arg + “\015\012” }
> 
> def call\_a\_routine(routine, argument)  
> puts “Hello Ruby, World”
> 
> What exactly are you really trying to do?
> 
> ## -austin
> 
> Austin Z. \* removed\_email\_address@domain.invalid  
> \* Alternate: removed\_email\_address@domain.invalid

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!
