Why? Blocks

Why can I not do this:

c = [1,2,3,4,5,6,6,7,8,10]
b = {|aVal| aVal / 5 == 0}
d = c.select(b)

I would like to store sections of code so I don't have to repeat myself...

Julian.

b = Proc.new {|aVal| aVal / 5 == 0}
d = c.select(&b)

- Jamis

···

On Sep 8, 2005, at 8:49 PM, Julian Leviston wrote:

Why can I not do this:

c = [1,2,3,4,5,6,6,7,8,10]
b = {|aVal| aVal / 5 == 0}
d = c.select(b)

I would like to store sections of code so I don't have to repeat myself...

What's the significance of '&b' vs 'b'?

···

On 9/8/05, Jamis Buck <jamis@37signals.com> wrote:

On Sep 8, 2005, at 8:49 PM, Julian Leviston wrote:

> Why can I not do this:
>
> c = [1,2,3,4,5,6,6,7,8,10]
> b = {|aVal| aVal / 5 == 0}
> d = c.select(b)
>
> I would like to store sections of code so I don't have to repeat
> myself...

b = Proc.new {|aVal| aVal / 5 == 0}
d = c.select(&b)

You ROCK :slight_smile:

Man THANNNNKKKSSSS :slight_smile:

It's gonna take me a little bit of study to work out blocks, lambdas and Proc objects! Far out.
But how awesome that Ruby can do that. Awesome! I'm like excited as... :slight_smile: Every time I come up against a new problem domain, I get these awesome answers from these awesome people! {BIG VIRTUAL HUGS} thanks guys.

Julian.

···

On 09/09/2005, at 12:52 PM, Jamis Buck wrote:

On Sep 8, 2005, at 8:49 PM, Julian Leviston wrote:

Why can I not do this:

c = [1,2,3,4,5,6,6,7,8,10]
b = {|aVal| aVal / 5 == 0}
d = c.select(b)

I would like to store sections of code so I don't have to repeat myself...

b = Proc.new {|aVal| aVal / 5 == 0}
d = c.select(&b)

- Jamis

Hi --

···

On Fri, 9 Sep 2005, Joe Van Dyk wrote:

On 9/8/05, Jamis Buck <jamis@37signals.com> wrote:

On Sep 8, 2005, at 8:49 PM, Julian Leviston wrote:

Why can I not do this:

c = [1,2,3,4,5,6,6,7,8,10]
b = {|aVal| aVal / 5 == 0}
d = c.select(b)

I would like to store sections of code so I don't have to repeat
myself...

b = Proc.new {|aVal| aVal / 5 == 0}
d = c.select(&b)

What's the significance of '&b' vs 'b'?

&b means that it's serving as the code block for this method call. b
means the Proc object is just a regular argument to the method.

David

--
David A. Black
dblack@wobblini.net

> b = Proc.new {|aVal| aVal / 5 == 0}
> d = c.select(&b)

What's the significance of '&b' vs 'b'?

It tells ruby that parameter is to take the place
of the block, rather than be passed as one of the
arguments to the method.

Regards,

Bill

···

From: "Joe Van Dyk" <joevandyk@gmail.com>

There is a similiarity between the star and the ampersand operator. A
star packs and unpacks arrays, while the ampersand packs and unpacks
code. E.g.

$ cat star-ampersand.rb
def another_method(a, b, c)
  yield(:a=>a, :b => b, :c => c)
end

def take_block(*args, &block)
  another_method(*args, &block)
end

take_block(1,2,3) do | hash | p hash end

$ ruby star-ampersand.rb
{:b=>2, :c=>3, :a=>1}

regards,

Brian

···

On 09/09/05, Joe Van Dyk <joevandyk@gmail.com> wrote:

On 9/8/05, Jamis Buck <jamis@37signals.com> wrote:
> On Sep 8, 2005, at 8:49 PM, Julian Leviston wrote:
>
> > Why can I not do this:
> >
> > c = [1,2,3,4,5,6,6,7,8,10]
> > b = {|aVal| aVal / 5 == 0}
> > d = c.select(b)
> >
> > I would like to store sections of code so I don't have to repeat
> > myself...
>
> b = Proc.new {|aVal| aVal / 5 == 0}
> d = c.select(&b)

What's the significance of '&b' vs 'b'?

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/