# Confusion with Enum#with_object block argument construct

C:\>irb
irb(main):001:0> module Enumerable
irb(main):002:1> def diff
<ons(2).with_object([]){|x,array| array << x[1] - x[0]}
irb(main):004:2> 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 `<main>'

# 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/.

Can anybody help me here to clear out the confusions?

Thanks

···

--
Posted via http://www.ruby-forum.com/.

the array at with_object collects the elements but:
[1,3,10,5].each_cons(2).map{|x,y| y - x} #=> [2, 7, -5]

works fine too

···

--
Posted via http://www.ruby-forum.com/.

Hans Mackowiak wrote in post #1095038:

the array at with_object collects the elements but:
[1,3,10,5].each_cons(2).map{|x,y| y - x} #=> [2, 7, -5]

works fine too

Can anyone help me on my question- A. This is a but confusing for me
being a new ruby student?

Thanks

···

--
Posted via http://www.ruby-forum.com/.

Can anyone help me on my question- A. This is a but confusing for me
being a new ruby student?

I have a little difficulty understanding exactly what you're asking, but
I'll try to help:

(A) from the source code, Is there any way to get the right construct

rather blind try.

I started by Googling 'ruby array each_cons', which lead me to <
http://ruby-doc.org/core-1.9.3/Enumerable.html> which says that each_cons
without a block returns an enumerator. I then searched for 'ruby
enumerator with_object' which lead me to <
http://ruby-doc.org/core-1.9.3/Enumerator.html#method-i-with_object>

The method signature for Enumerator#with_object states:
with_object(obj) {|(*args), obj| ... }
which tells me straight away that the _last_ block parameter is the object
I passed to #with_object, and the other args are the elements over which
I'm iterating.

The example given is slightly abstract, as it demonstrates the version of
#with_object without a block argument, but it still shows that:
...with_object("foo") {|x,string| puts "#{string}: #{x}" }
outputs
foo: x1
foo: x2
etc.

But in the doc there was several methods for which no example given.

If there is no example, you should read the description. If there is no
description and no example, the simplest thing to do is either: try it
yourself, or come here and ask us.

Poor or missing documentation, especially in the core libraries, is
definitely grounds for filing a bug report.

···

On 4 February 2013 17:02, Love U Ruby <lists@ruby-forum.com> wrote:

--
Matthew Kerwin, B.Sc (CompSci) (Hons)
http://matthew.kerwin.net.au/
ABN: 59-013-727-651

"You'll never find a programming language that frees
you from the burden of clarifying your ideas." - xkcd

Matthew Kerwin wrote in post #1095078:

···

On 4 February 2013 17:02, Love U Ruby <lists@ruby-forum.com> wrote:

(A) from the source code, Is there any way to get the right construct

rather blind try.

I started by Googling 'ruby array each_cons', which lead me to <
http://ruby-doc.org/core-1.9.3/Enumerable.html> which says that
each_cons
without a block returns an enumerator. I then searched for 'ruby
enumerator with_object' which lead me to <
http://ruby-doc.org/core-1.9.3/Enumerator.html#method-i-with_object>

@Matthew - Thank you very much for your help!

--
Posted via http://www.ruby-forum.com/.

As well as filling in the comments on the page. That is generally how
the documentation get better over time.

···

On Mon, Feb 4, 2013 at 1:20 AM, Matthew Kerwin <matthew@kerwin.net.au> wrote:

Poor or missing documentation, especially in the core libraries, is
definitely grounds for filing a bug report.

Well you should thank him for doing your searching and reading for you.

···

On Mon, Feb 4, 2013 at 4:14 AM, Love U Ruby <lists@ruby-forum.com> wrote:

Matthew Kerwin wrote in post #1095078:

On 4 February 2013 17:02, Love U Ruby <lists@ruby-forum.com> wrote:

(A) from the source code, Is there any way to get the right construct

rather blind try.

I started by Googling 'ruby array each_cons', which lead me to <
http://ruby-doc.org/core-1.9.3/Enumerable.html> which says that
each_cons
without a block returns an enumerator. I then searched for 'ruby
enumerator with_object' which lead me to <
http://ruby-doc.org/core-1.9.3/Enumerator.html#method-i-with_object>

@Matthew - Thank you very much for your help!

To be honest, I just wanted to look up #each_cons. I know #each_slice ,
and wanted to see if/how it was different.

···

On 4 February 2013 20:19, tamouse mailing lists <tamouse.lists@gmail.com>wrote:

On Mon, Feb 4, 2013 at 4:14 AM, Love U Ruby <lists@ruby-forum.com> wrote:
> @Matthew - Thank you very much for your help!

Well you should thank him for doing your searching and reading for you.

--
Matthew Kerwin, B.Sc (CompSci) (Hons)
http://matthew.kerwin.net.au/
ABN: 59-013-727-651

"You'll never find a programming language that frees
you from the burden of clarifying your ideas." - xkcd