From: Arup Rakshit Date: 2013-02-03T18:30:41+09:00 Subject: Confusion with Enum#with_object block argument construct C:\>irb irb(main):001:0> module Enumerable irb(main):002:1> def diff end irb(main):005:1> end => nil irb(main):006:0> [1,3,10,5].diff => [2, 7, -5] Try-I ===== irb(main):007:0> [1,3,10,5].each_cons(2).with_object([]){|array,x| array << x[1] - x[0]} NoMethodError: undefined method `-' for nil:NilClass from (irb):7:in `block in irb_binding' from (irb):7:in `each' from (irb):7:in `each_cons' from (irb):7:in `with_object' from (irb):7 from C:/Ruby193/bin/irb:12:in `
' Try-II ====== irb(main):008:0> [1,3,10,5].each_cons(2).with_object([]){|x,array| array << x[1] - x[0]} => [2, 7, -5] irb(main):009:0> In the above code I was trying to see which is the perfect "block argument list" construct. As usual got the error from "Try-I", but "Try-II" construct is right. Now my question is - (A) from the source code, Is there any way to get the right construct rather blind try. Yes I know that - I should refer the examples. But in the doc there was several methods for which no example given. My question is in that case how should be my approach? (B) How does the array object inside "with_object([])" helping here to calculate difference? Wanted to know the functional perspective of "with_object? Thanks -- Posted via http://www.ruby-forum.com/.