Enumerable Integer? Nah. Integer#times Enumerator!

Discussion in 'Ruby' started by Trans, Dec 27, 2007.

  1. Trans

    Trans Guest

    Just a little FYI.

    Not too long ago I noted this suggestion:

    class Integer
    alias_method :each, :times
    include Enumerable
    end

    Which is kind of cool in that allows some convenient statements like

    10.collect { |i| "No. #{i}" }
    10.select { |i| i % 3 == 0 }

    And so forth. This is nice, but, of course, the downside is that this
    clutters up Integer with all these Enumerable methods.

    Thankfully, and this is the FYI, Ruby 1.9 has Integer#times returning
    an Enumerator. So most of these same functionality can be had just by
    inserting #times in between:

    10.times.collect { |i| "No. #{i}" }
    10.times.select { |i| i % 3 == 0 }

    This is great IMHO, so I thought I'd share. My only aside is the
    thought that perhaps there's still a good reason to add Integer#to_a --
    to save us the intermediate object of times.to_a. Or is that too much
    of a YAGNI?

    T.
    Trans, Dec 27, 2007
    #1
    1. Advertising

  2. Trans

    George Guest

    On Dec 28, 2007 8:55 AM, Trans <> wrote:
    > Just a little FYI.
    >
    > Not too long ago I noted this suggestion:
    >
    > class Integer
    > alias_method :each, :times
    > include Enumerable
    > end
    >
    > Which is kind of cool in that allows some convenient statements like
    >
    > 10.collect { |i| "No. #{i}" }
    > 10.select { |i| i % 3 == 0 }
    >
    > And so forth. This is nice, but, of course, the downside is that this
    > clutters up Integer with all these Enumerable methods.
    >
    > Thankfully, and this is the FYI, Ruby 1.9 has Integer#times returning
    > an Enumerator. So most of these same functionality can be had just by
    > inserting #times in between:
    >
    > 10.times.collect { |i| "No. #{i}" }
    > 10.times.select { |i| i % 3 == 0 }
    >
    > This is great IMHO, so I thought I'd share. My only aside is the
    > thought that perhaps there's still a good reason to add Integer#to_a --
    > to save us the intermediate object of times.to_a. Or is that too much
    > of a YAGNI?


    Hi Trans,

    Thanks for the FYI. For what it's worth, I can't say I'm really a fan
    of 10.times.select--it doesn't seem very clear to me what's being
    iterated over. 10.select even less so. I think good ol'
    (0...10).select looks better. *shrug*

    Similarly, 10.to_a returning [0,1,...,9] doesn't really feel right to
    me. In fact, I'd probably expect 10.to_a to do the same as Array(10)
    (and changing the latter may well break things).

    Just my thoughts.

    Regards,
    George.
    George, Dec 28, 2007
    #2
    1. Advertising

  3. 2007/12/27, Trans <>:
    > Just a little FYI.
    >
    > Not too long ago I noted this suggestion:
    >
    > class Integer
    > alias_method :each, :times
    > include Enumerable
    > end
    >
    > Which is kind of cool in that allows some convenient statements like
    >
    > 10.collect { |i| "No. #{i}" }
    > 10.select { |i| i % 3 == 0 }
    >
    > And so forth. This is nice, but, of course, the downside is that this
    > clutters up Integer with all these Enumerable methods.


    Well, in the old days (1.8.x) you could do this which does not look
    too different from the 1.9 solution you show below

    10.to_enum:)times).collect { |i| "No. #{i}" }
    10.to_enum:)times).select { |i| i % 3 == 0 }

    > Thankfully, and this is the FYI, Ruby 1.9 has Integer#times returning
    > an Enumerator. So most of these same functionality can be had just by
    > inserting #times in between:
    >
    > 10.times.collect { |i| "No. #{i}" }
    > 10.times.select { |i| i % 3 == 0 }
    >
    > This is great IMHO, so I thought I'd share. My only aside is the
    > thought that perhaps there's still a good reason to add Integer#to_a --
    > to save us the intermediate object of times.to_a. Or is that too much
    > of a YAGNI?


    I'd rather use 10.times.to_a instead of 10.to_a because the latter
    seems too unobvious to me.

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
    Robert Klemme, Dec 28, 2007
    #3
  4. Trans

    Trans Guest

    On Dec 28, 12:01 pm, "Robert Klemme" <>
    wrote:

    > I'd rather use 10.times.to_a instead of 10.to_a because the latter
    > seems too unobvious to me.


    Understandable. But obvious isn't always the most important factor. A
    straight #to_a is vastly more efficient.

    T.
    Trans, Dec 28, 2007
    #4
  5. On 28.12.2007 19:30, Trans wrote:
    >
    > On Dec 28, 12:01 pm, "Robert Klemme" <>
    > wrote:
    >
    >> I'd rather use 10.times.to_a instead of 10.to_a because the latter
    >> seems too unobvious to me.

    >
    > Understandable. But obvious isn't always the most important factor. A
    > straight #to_a is vastly more efficient.


    Good point. I'd like to point out that the overhead completely depends
    on the number where #to_a is invoked on. For small numbers the overhead
    is likely significant. But for large numbers, especially BigNums I'd
    say the overhead is negligible. But I guess, in reality this would be
    more often used with rather smallish numbers. So yes, this should be
    taken into account.

    Kind regards

    robert
    Robert Klemme, Dec 29, 2007
    #5
  6. On Dec 28, 2007 1:30 PM, Trans <> wrote:
    >
    >
    > On Dec 28, 12:01 pm, "Robert Klemme" <>
    > wrote:
    >
    > > I'd rather use 10.times.to_a instead of 10.to_a because the latter
    > > seems too unobvious to me.

    >
    > Understandable. But obvious isn't always the most important factor. A
    > straight #to_a is vastly more efficient.


    Keep in mind that in 1.8

    10.to_a #=> [10]

    which also gives a deprecation warning, because in 1.9 Object#to_a and
    several other implementations of to_a have been removed.




    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
    Rick DeNatale, Dec 29, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWF2cmlja18xMDE=?=

    SetAuthCookie works some times and fails some times?

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    498
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 23, 2006
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Forms Authentication Fails some times and not some times???

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    481
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 28, 2006
  3. Daniel Sheppard
    Replies:
    0
    Views:
    96
    Daniel Sheppard
    Sep 16, 2005
  4. Alex Baranosky
    Replies:
    5
    Views:
    110
    Robert Klemme
    Mar 25, 2010
  5. Roger Pack
    Replies:
    12
    Views:
    222
    Brian Candler
    May 13, 2011
Loading...

Share This Page