I’m just learning ruby and so far everything has been great (using the
Thomas and Hunt book), but I don’t quite understand how proc objects
(and its call method) relate to code blocks. If anyone could explain I
would greatly appriciate it.
procs can be used as standalone ‘closures’ and passed around/called as
callable functions (via the returned proc object).
ie:
def testFunc( obj )
obj.call
end
bleah = proc { puts “something” }
testFunc( bleah)
···
something
They can substitute as blocks by passing the proc (with a preceeding &)
as a method parameter to something expecting a block. This turns the
proc into a block for that method.
ie:
foo = proc { |val| puts val }
a = [1,2,3]
a.each( &foo )
1
2
3
I’m missing a couple other variations, but you should get the idea. You
might want to dig into the Thomas/Hunt book a bit more.
Robert Warning wrote:
I’m just learning ruby and so far everything has been great (using the
Thomas and Hunt book), but I don’t quite understand how proc objects
(and its call method) relate to code blocks. If anyone could explain I
would greatly appriciate it.
I’m just learning ruby and so far everything has been great (using the
Thomas and Hunt book), but I don’t quite understand how proc objects
(and its call method) relate to code blocks. If anyone could explain I
would greatly appriciate it.
It’s reasonably simple. The following code snippets are equivalent:
···
+++++++++++++++++++++++
array.each do |e|
puts “Data: #{e}”
end
=====================
block = proc { |e| puts “Data: #{e}” }
array.each(&block)
+++++++++++++++++++++++
From the method’s side, these are equivalent:
(e.g.
count(5…15) { |i| i % 4 == 0 } ==> 2
p = proc { |i| i % 4 == 0 }
count(5…15, &p) ==> 2
)
+++++++++++++++++++++++
def count(range)
result = 0
for i in range
result += 1 if yield i
end
result
end
=====================
def count(range, &block)
result = 0
for i in range
result += 1 if block.call(i) # or block[i]
end
result
end
+++++++++++++++++++++++
Let the corrections flow in!
Cheers,
Gavin
Patrick Bennett wrote:
procs can be used as standalone ‘closures’ and passed around/called as
callable functions (via the returned proc object).
ie:def testFunc( obj )
obj.call
endbleah = proc { puts “something” }
testFunc( bleah)something
They can substitute as blocks by passing the proc (with a preceeding
&) as a method parameter to something expecting a block. This turns
the proc into a block for that method.
ie:
foo = proc { |val| puts val }
a = [1,2,3]
a.each( &foo )1
2
3I’m missing a couple other variations, but you should get the idea.
You might want to dig into the Thomas/Hunt book a bit more.Robert Warning wrote:
I’m just learning ruby and so far everything has been great (using
the Thomas and Hunt book), but I don’t quite understand how proc
objects (and its call method) relate to code blocks. If anyone could
explain I would greatly appriciate it.
Thanks, blocks in the sence that Ruby uses them is still kinda wierd to
me even after going over it a few times. I guess I’m too acustmed to C
style blocks. The closest thing ruby has to C style blocks is the ALGOL
style END blocks. Don’t take that as a fact that I know alot about
programing languages its just one of those quirky things I know from
looking at some ALGOL (yes that 1960’s thingamajiger) source code once.
My only real experiance with programming is with BASIC php and very
little perl. When I was 11 I attempted C after BASIC and had a brain
overload. Havn’t touched C since then :).