Range#step behavior is different from core Ruby #6297

Closed
joshuaclayton opened this Issue May 13, 2012 · 6 comments

3 participants

@joshuaclayton

When calling Range#step without a block, instead of returning an Enumerator, an array is returned. Why is this? I'm all for ActiveSupport extending behavior of core Ruby libraries, but to completely change the value that's returned seems wrong.

If this can't be changed, is there a way to opt out of this specific behavior?

>> (0..10).step(2)
=> #<Enumerator: 0..10:step(2)>
>> require 'active_support/core_ext'
=> true
>> (0..10).step(2)
=> [0, 2, 4, 6, 8, 10]
@carlosantoniodasilva
Ruby on Rails member

According to the code, it overrides Range#step to handle the case without blocks. I don't know why this code was added before, but could be to handle some weird Ruby cases in older versions, where it didn't handle this method without a block. This commit 99c6482 added it 5 years ago.

There's no way to opt-out of this particular behavior, unless you require all stuff you want manually. Anyway, I believe the monkey patch could be removed, as one could just call to_a yourself if required.

@jeremy thoughs on this one?

@spastorino
Ruby on Rails member

@carlosantoniodasilva @jeremy I've just seen your comment Carlos. For me the blockless_step.rb code doesn't make sense anymore. If you feel that I'm wrong feel free to revert it :).

@carlosantoniodasilva
Ruby on Rails member

I'm just fine with the removal, thanks bro :)

@joshuaclayton

You guys are damn fast! I was going to make a pull request if everyone agreed it could be removed; thanks!

@spastorino
Ruby on Rails member

@joshuaclayton sorry for stealing your commit so :(

@joshuaclayton

@spastorino no worries, just glad it got fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment