Newbie question: Small blocks

Hello folks,

sorry for this newbie question, but I saw somewhere that there is a way
to
make a small block without using pipes " | ".

It was something like [0, 1, 2].each { } , with some char inside the
brackets, like &, but I can’t remember it. anyone knows how to use it?

Thanks
Dante

Dante R. wrote:

Hello folks,

sorry for this newbie question, but I saw somewhere that there is a way to
make a small block without using pipes " | ".

It was something like [0, 1, 2].each { } , with some char inside the
brackets, like &, but I can’t remember it. anyone knows how to use it?

Maybe this?

class Symbol
def to_proc
proc {|x,*args| x.send(self, *args)}
end
end

[1,2,3].map(&:to_s) # ==> [“1”, “2”, “3”]

Dante R. wrote:

It was something like [0, 1, 2].each { } , with some char inside the
brackets, like &, but I can’t remember it. anyone knows how to use it?

If I guess correctly, ActiveSupport has a cute little tweak that permits
this:

p (0…10).to_a.map(&:to_s)

Any place you can write {|x| x.y}, you can shorten that to (&:y). This
works
great with ActiveRecord, to convert a list of database records into a
list
of one of its fields:

p User.find(:all).map(&:name)

make a small block without using pipes " | ".

It was something like [0, 1, 2].each { } , with some char
inside the
brackets, like &, but I can’t remember it. anyone knows how to use it?

Thanks
Dante

[1,2].each do |elem|
elem.do_something
end

Is equivalent to:

[1,2].each do {|elem| elem.do_something}

Felix

Joel VanderWerf wrote:

[1,2,3].map(&:to_s) # ==> [“1”, “2”, “3”]
proc {|x,*args| x.send(self, *args)}

*args?! Nobody told me you could pass args in too!

that’s just what I was looking for. thanks the both of you

Dante

You can type any block with the { } or the do…end way. You don’t have
to have the pipes, but without them you don’t have a way to pass items
of your collection into the block. So for example

array.each { puts “HELLO” } <— no pipes, but no data

The only thing I can think of where you would have seen the & would be
something like

irb(main):001:0> (0…5).each { 0 & 5 }
=> 0…5
irb(main):002:0>

~Jeremy

Phlip wrote:

Dante R. wrote:

It was something like [0, 1, 2].each { } , with some char inside the
brackets, like &, but I can’t remember it. anyone knows how to use it?

If I guess correctly, ActiveSupport has a cute little tweak that permits
this:

p (0…10).to_a.map(&:to_s)

Any place you can write {|x| x.y}, you can shorten that to (&:y). This
works
great with ActiveRecord, to convert a list of database records into a
list
of one of its fields:

p User.find(:all).map(&:name)

I totally forgot about that one. I guess I was thinking just Ruby and
not anything with rails. :slight_smile:

~Jeremy

make a small block without using pipes " | ".
[1,2].each do |elem|

David

Whoops.

Hi –

On Tue, 11 Sep 2007, Felix W. wrote:

make a small block without using pipes " | ".
elem.do_something
end

Is equivalent to:

[1,2].each do {|elem| elem.do_something}

You don’t want the ‘do’ on the braces version though. (And yes, I’ve
done it occasionally :slight_smile:

David