-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement String#split with block #3702
Implement String#split with block #3702
Conversation
@kostya Dammit, should have searched for it before ;) Sorry, for the noise. (I am actually using the block version to speed-up things...) |
it was not merged, and i also want a version which yield slice, for speed |
@kostya Yeah, my version yields Anyhow, if it's not wanted, I'll keep my monkey patch^W^Wextension for now ;) |
@@ -738,6 +738,7 @@ describe "String" do | |||
assert { " foo bar".split(' ').should eq(["", "", "", "foo", "", "bar"]) } | |||
assert { " foo bar\n\t baz ".split(' ').should eq(["", "", "", "foo", "", "", "bar\n\t", "", "baz", "", "", ""]) } | |||
assert { " foo bar\n\t baz ".split.should eq(["foo", "bar", "baz"]) } | |||
assert { " foo bar\n\t baz ".split(1).should eq([" foo bar\n\t baz "]) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm reopening the PR. The problem of #2541 is avoided because it yields String instead of an unexpected Slice —which would fit better as String 👍 from me! |
yielding slice is ok, because it internal method which i need to use for speed, common method still return strings. |
I'm also OK with this addition. Eventually I'd also like to be able to get an @kostya We could add a separate method that yields slices, with a different name (maybe |
# water's sound | ||
# " | ||
# old_pond.split { |s| ary << s }; ary # => ["Old", "pond", "a", "frog", "leaps", "in", "water's", "sound"] | ||
# old_pond.split(3) { |s| ary << s }; ary # => ["Old", "pond", "a frog leaps in\n water's sound\n"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ary
should probably be cleared before each split. This applies to this example and other examples below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@asterite Good catch! Fixed.
@asterite Ready to merge. Do you want me to squash the commits? |
@splattael No problem, I think the separate commits look good :-) I'll merge it soon. Thank you! |
Hi,
this PR enhances all flavors of
String#split
with a block.After this PR, you can pass a block to
String#split
:Additional tests were not added because all non-block versions of
String#split
exerciseString#split(..., &block)
. (Only one test for an edge case was added, though)Kind regards,
Peter