Enumerator in ruby 1.9

One could write

module Enumerable
def sum
inject(0) {|f,x| f+x}
end
end

And we have

puts [1,2,3,4].sum # => 10

But it works only for numbers.

I would like to write more general code:

module Enumerable
def sum
each.skip_first.inject(first) {|f,x| f+x}
end
end

so we have:

puts [1,2,3,4].sum #=> 10
puts [‘a’,‘b’,‘c’].sum #=> ‘abc’

  1. Is it a good idea to add method '‘first’ to Enumerable?
  2. Please, show me the right code for Enumerator#skip_first(n=1)
    (ruby1.9)

Thanks!

Hi –

On Thu, 13 Mar 2008, Artem V. wrote:

But it works only for numbers.

puts [‘a’,‘b’,‘c’].sum #=> ‘abc’

  1. Is it a good idea to add method '‘first’ to Enumerable?
  2. Please, show me the right code for Enumerator#skip_first(n=1) (ruby1.9)

If you don’t give an argument to inject, it uses the first element in
the collection. So you can just do:

%w{a b c}.inject {|acc,e| acc + e }

David

Sorry, I’ve found than ‘inject’ has form without argument and it does
the job.

module Enumerable
def sum
inject {|f,x| f+x}
end
end

But i am still interested in:

  1. Is it a good idea to add method '‘first’ to Enumerable?
  2. Please, show me the right code for Enumerator#skip_first(n=1)
    (ruby1.9)

And one more question offtopic:

I see my messages duplicated in list. I use gmail.com
What should I do to stop duplicating my messages?

On Thu, Mar 13, 2008 at 12:33 PM, Artem V.
[email protected] wrote:

And one more question offtopic:

I see my messages duplicated in list. I use gmail.com

I assume the following: You send a message to the ruby-talk, GMail
saves a copy in the conversation, ruby-talk sends you your email.
Not sure if it is like that, but that’s how I explain it to me.

What should I do to stop duplicating my messages?

I am using Better GMail and delete my duplicated emails when I decide
to archive a ruby-talk conversation.

For example


module Enumerable
class Enumerator
def skip_first(n=1)
n.times {puts “Skiping #{self.next}”}
self
end
end
end

puts [1,2,3,4].each.skip_first(2).map{|i| i}

Outputs

Skiping 1
Skiping 2
1
2
3
4

(ruby 1.9.0 (2007-12-25 revision 14709) [i386-mswin32])

So it does not skips in fact. Enumerator restarts inside method map.
It looks strange for me.

Can anyone help me to write ‘skip_first’ method that works?

Artem

On Mar 13, 2008, at 6:33 AM, Artem V. wrote:

But i am still interested in:

  1. Is it a good idea to add method '‘first’ to Enumerable?

You can use Ruby 1.9’s take() for this:

$ ruby_dev -ve ‘p((1…10).take(1).first)’
ruby 1.9.0 (2008-03-01 revision 15664) [i686-darwin9.2.0]
1

  1. Please, show me the right code for Enumerator#skip_first(n=1)
    (ruby1.9)

Use take()'s cousin drop():

$ ruby_dev -ve ‘p((1…10).drop(1))’
ruby 1.9.0 (2008-03-01 revision 15664) [i686-darwin9.2.0]
[2, 3, 4, 5, 6, 7, 8, 9, 10]

James Edward G. II

2008/3/13, James G. [email protected]:

1

Thanks.

James Edward G. II

I want to have ‘drop’ for Enumerator so that no new collection was
created.
It’s important when collection is big one.