Why? Blocks

  • Thread starter Julian Leviston
  • Start date
J

Julian Leviston

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

Jamis Buck

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
 
J

Joe Van Dyk

On Sep 8, 2005, at 8:49 PM, Julian Leviston wrote:
=20
Why can I not do this:

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

I would like to store sections of code so I don't have to repeat
myself...
=20
b =3D Proc.new {|aVal| aVal / 5 =3D=3D 0}
d =3D c.select(&b)

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

David A. Black

Hi --

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
 
B

Bill Kelly

From: "Joe Van Dyk said:
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
 
B

Brian Schröder

Why can I not do this:

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

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

b =3D Proc.new {|aVal| aVal / 5 =3D=3D 0}
d =3D c.select(&b)
=20
What's the significance of '&b' vs 'b'?
=20
=20

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=3D>a, :b =3D> b, :c =3D> 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=3D>2, :c=3D>3, :a=3D>1}

regards,

Brian


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

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

Julian Leviston

You ROCK :)

Man THANNNNKKKSSSS :)

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... :) 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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,511
Latest member
svareza

Latest Threads

Top