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
on 2013-02-03 10:30
on 2013-02-03 22:24
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
on 2013-02-04 08:02
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
on 2013-02-04 08:20
On 4 February 2013 17:02, Love U Ruby <lists@ruby-forum.com> wrote: > > 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#met... 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. -- 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
on 2013-02-04 11:14
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#met... > @Matthew - Thank you very much for your help!
on 2013-02-04 11:18
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. As well as filling in the comments on the page. That is generally how the documentation get better over time.
on 2013-02-04 11:20
On Mon, Feb 4, 2013 at 4:14 AM, Love U Ruby <lists@ruby-forum.com> wrote: >> enumerator with_object' which lead me to < >> http://ruby-doc.org/core-1.9.3/Enumerator.html#met... >> > > @Matthew - Thank you very much for your help! Well you should thank him for doing your searching and reading for you.
on 2013-02-04 11:26
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. > To be honest, I just wanted to look up #each_cons. I know #each_slice , and wanted to see if/how it was different. ;) -- 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
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.