[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Enumerator in ruby 1.9.

Artem Voroztsov

3/13/2008 11:19:00 AM

# 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!

7 Answers

David A. Black

3/13/2008 11:27:00 AM

0

Hi --

On Thu, 13 Mar 2008, Artem Voroztsov wrote:

> # 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)

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

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.r... for details. Berlin dates coming soon!

Artem Voroztsov

3/13/2008 11:33:00 AM

0

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?

Thomas Wieczorek

3/13/2008 11:46:00 AM

0

On Thu, Mar 13, 2008 at 12:33 PM, Artem Voroztsov
<artem.voroztsov@gmail.com> 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.

Artem Voroztsov

3/13/2008 12:32:00 PM

0

2008/3/13, Thomas Wieczorek <wieczo.yo@googlemail.com>:
> On Thu, Mar 13, 2008 at 12:33 PM, Artem Voroztsov
> <artem.voroztsov@gmail.com> 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.
>
Thanks!

Artem Voroztsov

3/13/2008 12:44:00 PM

0

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

James Gray

3/13/2008 12:46:00 PM

0

On Mar 13, 2008, at 6:33 AM, Artem Voroztsov 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

> 2) 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 Gray II

Artem Voroztsov

3/13/2008 1:11:00 PM

0

2008/3/13, James Gray <james@grayproductions.net>:
> On Mar 13, 2008, at 6:33 AM, Artem Voroztsov 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
>

Thanks.


> > 2) 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 Gray II
>
>

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