Skip to content

Implement Enumerable#slice_after#3351

Merged
yorickpeterse merged 1 commit intorubinius:2.2from
jsyeo:slice-after
Mar 11, 2015
Merged

Implement Enumerable#slice_after#3351
yorickpeterse merged 1 commit intorubinius:2.2from
jsyeo:slice-after

Conversation

@jsyeo
Copy link
Member

@jsyeo jsyeo commented Mar 8, 2015

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 and nil?s.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)"
end

I 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!undefined.equal?(pattern) is more natural.

@jsyeo jsyeo force-pushed the slice-after branch 2 times, most recently from 10f8afd to 0cf5e0b Compare March 11, 2015 17:48

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should've been more clear with this (my bad), I meant whitespace between every line, not just the one preceding Enumerator.new do ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh... okay got it. 👍

@jsyeo
Copy link
Member Author

jsyeo commented Mar 11, 2015

@yorickpeterse I've added the newlines. 😄

@yorickpeterse yorickpeterse merged commit ef0d44d into rubinius:2.2 Mar 11, 2015
@jsyeo jsyeo deleted the slice-after branch March 12, 2015 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants