[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

returning Procs from methods?

Giles Bowkett

11/29/2006 11:11:00 AM

I've got something which basically does this:

some_string.scan(/(whatever)/).each &do_stuff

where &do_stuff is a Proc.

currently there's a case/when that assigns different Procs to do_stuff
depending on the value of some_string.

what I want to do is something like this:

some_string.scan(/(whatever)/).each do_stuff

where do_stuff() is a method which figures out what Proc to return to
the each().

however, I can't seem to build a method which returns a Proc.

can it be done?

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...

5 Answers

Robert Feldt

11/29/2006 11:19:00 AM

0

On 11/29/06, Giles Bowkett <gilesb@gmail.com> wrote:
> I've got something which basically does this:
>
> some_string.scan(/(whatever)/).each &do_stuff
>
> where &do_stuff is a Proc.
>
> currently there's a case/when that assigns different Procs to do_stuff
> depending on the value of some_string.
>
> what I want to do is something like this:
>
> some_string.scan(/(whatever)/).each do_stuff
>
> where do_stuff() is a method which figures out what Proc to return to
> the each().
>
> however, I can't seem to build a method which returns a Proc.
>
> can it be done?
>
sure

def do_stuff
lambda {|a| a+1}
end

b = [1,2].map(&do_stuff)

/Robert Feldt

Martin DeMello

11/29/2006 11:22:00 AM

0

On 11/29/06, Giles Bowkett <gilesb@gmail.com> wrote:

> however, I can't seem to build a method which returns a Proc.
>
> can it be done?

Yes, but you still need to prefix it with a & when passing it to a
method that expects a block.

irb(main):001:0> def foo
irb(main):002:1> lambda {|x| x+1}
irb(main):003:1> end
=> nil
irb(main):004:0> (1..10).map foo
ArgumentError: wrong number of arguments (1 for 0)
from (irb):4:in `map'
from (irb):4
irb(main):005:0> (1..10).map &foo
=> [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]


martin

Giles Bowkett

11/29/2006 11:22:00 AM

0

awesome! thank you.

On 11/29/06, Robert Feldt <robert.feldt@gmail.com> wrote:
> On 11/29/06, Giles Bowkett <gilesb@gmail.com> wrote:
> > I've got something which basically does this:
> >
> > some_string.scan(/(whatever)/).each &do_stuff
> >
> > where &do_stuff is a Proc.
> >
> > currently there's a case/when that assigns different Procs to do_stuff
> > depending on the value of some_string.
> >
> > what I want to do is something like this:
> >
> > some_string.scan(/(whatever)/).each do_stuff
> >
> > where do_stuff() is a method which figures out what Proc to return to
> > the each().
> >
> > however, I can't seem to build a method which returns a Proc.
> >
> > can it be done?
> >
> sure
>
> def do_stuff
> lambda {|a| a+1}
> end
>
> b = [1,2].map(&do_stuff)
>
> /Robert Feldt
>
>


--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...

Martin DeMello

11/29/2006 11:24:00 AM

0

On 11/29/06, Robert Feldt <robert.feldt@gmail.com> wrote:
> >
> sure
>
> def do_stuff
> lambda {|a| a+1}
> end
>
> b = [1,2].map(&do_stuff)

lambda {|a| a+1} is the new "hello world" :)

martin

Mike Harris

11/29/2006 9:42:00 PM

0

Giles Bowkett wrote:

> I've got something which basically does this:
>
> some_string.scan(/(whatever)/).each &do_stuff
>
> where &do_stuff is a Proc.
>
> currently there's a case/when that assigns different Procs to do_stuff
> depending on the value of some_string.
>
> what I want to do is something like this:
>
> some_string.scan(/(whatever)/).each do_stuff
>
> where do_stuff() is a method which figures out what Proc to return to
> the each().
>
> however, I can't seem to build a method which returns a Proc.
>
> can it be done?
>
The foo method returns the proc

def foo
lambda { |x| x + 2 }
end

foo.call(4) #6

def other_foo(&b)
b
end

var = other_foo { |x| x + 2 }
var.call(4) #6