From: Walle Wallen on
I have done it many times before, but this time I can't get it too work.
I'm starting to get a bit frustrating.
So, I'm trying to sum all elements from the second element in a Array
using inject. The problem is that inject returns nil every time. What am
I doing wrong?

parameters.each {|element| print element + ","} --> ?faq,using,rtorrent
parameters.each {|element| print element.class.to_s + ","} -->
String,String,String
parameters.class --> Array

Doesn't work.
(parameters[1]..parameters[-1]).inject {|result, element| result +
element} --> nil
Neither does.
(parameters[1]..parameters[-1]).to_a.inject {|result, element| result +
element} --> nil

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

From: Brian Candler on
Walle Wallen wrote:
> Doesn't work.
> (parameters[1]..parameters[-1]).inject {|result, element| result +

That's creating a new Range object: ("faq".."rtorrent")

What you want is an array slice:

parameters[1..-1].inject ...
--
Posted via http://www.ruby-forum.com/.

From: Walle Wallen on
Brian Candler wrote:
> Walle Wallen wrote:
>> Doesn't work.
>> (parameters[1]..parameters[-1]).inject {|result, element| result +
>
> That's creating a new Range object: ("faq".."rtorrent")
>
> What you want is an array slice:
>
> parameters[1..-1].inject ...

Thanks, it worked.

I did a small test in IRB, and it seems like my method should work.
Strange.
a = ["a", "b", "c"]
=> ["a", "b", "c"]
>> (a[1]..a[-1]).inject {|result, element| entry + result}
=> "bc"
--
Posted via http://www.ruby-forum.com/.

From: Jesús Gabriel y Galán on
On Mon, Mar 29, 2010 at 12:39 PM, Walle Wallen <walle.sthlm(a)gmail.com> wrote:
> Brian Candler wrote:
>> Walle Wallen wrote:
>>> Doesn't work.
>>> (parameters[1]..parameters[-1]).inject {|result, element| result +
>>
>> That's creating a new Range object: ("faq".."rtorrent")
>>
>> What you want is an array slice:
>>
>>    parameters[1..-1].inject ...
>
> Thanks, it worked.
>
> I did a small test in IRB, and it seems like my method should work.
> Strange.
> a = ["a", "b", "c"]
> => ["a", "b", "c"]
>>> (a[1]..a[-1]).inject {|result, element| entry + result}
> => "bc"

As Brian has explained, (a[1]..[a-1]) creates a range. When you
iterate a range, it calls succ starting on the first element until it
reaches the last one. In your example with a, b, c:

irb(main):006:0> ("a".."c").each {|s| p s}
"a"
"b"
"c"

You get the three elements of the array, by chance. Try changing that
to something else, for example:

irb(main):013:0> array = ["a", "d", "f"]
=> ["a", "d", "f"]
irb(main):014:0> (array[1]..array[-1]).inject {|result, element|
result + element}
=> "def"

In your original example, the words were quite far apart:

irb(main):005:0> ("faq".."rtorrent").each_with_index {|s, i| p s;
break if i > 20}
"faq"
"far"
"fas"
"fat"
"fau"
"fav"
"faw"
"fax"
"fay"
"faz"
"fba"
"fbb"
"fbc"
"fbd"
"fbe"
"fbf"
"fbg"
"fbh"
"fbi"
"fbj"
"fbk"
"fbl"

and so on until "rtorrent".

Jesus.

From: Walle Wallen on
Thanks Jesus, I get it now.
--
Posted via http://www.ruby-forum.com/.