Implement Enumerable#slice_after#3351
Conversation
kernel/common/enumerable.rb
Outdated
There was a problem hiding this comment.
Might I suggest the following instead:
if pattern_given && block_given?
raise ArgumentError, "cannot pass both pattern and block"
elsif pattern_given
block = Proc.new { |elem| pattern === elem }
elsif block_given?
# use given block
else
raise ArgumentError, "wrong number of arguments (0 for 1)"
endI think we should avoid allocating a bunch of unnecessary Arrays if we can (it seems like it would be a performance hit), and I think my example is still just as readable, if not a little more so. The example I gave could be collapsed a bit more into nested ifs, but I care less about that difference.
There was a problem hiding this comment.
Alright. How about this:
raise ArgumentError, "cannot pass both pattern and block" if pattern_given && block_given?
raise ArgumentError, "wrong number of arguments (0 for 1)" if !pattern_given && !block_given?
block = Proc.new { |elem| pattern === elem } if pattern_given
# rest of code
kernel/common/enumerable.rb
Outdated
There was a problem hiding this comment.
!undefined.equal?(pattern) is more natural.
10f8afd to
0cf5e0b
Compare
kernel/common/enumerable.rb
Outdated
There was a problem hiding this comment.
Probably should've been more clear with this (my bad), I meant whitespace between every line, not just the one preceding Enumerator.new do ...
|
@yorickpeterse I've added the newlines. 😄 |
Now that we have the specs for Enumerable#slice_after, we can implement it!
I hope I didn't confuse anyone with the crazy case when usage. Correct me if I am wrong, but I think it's more readable than a bunch of
ifs andnil?s.