From: Gavin Sinclair Date: 2002-08-16T13:39:43+09:00 Subject: Re: Hello everyone! > 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