Issue #9116 has been updated by Marcus Stollsteimer.


I'd like to revive the discussion about String#rsplit.

Here one use case I stumbled upon recently: splitting the digest off the end of a cookie (taken from Rack::Session::Cookie, see https://github.com/rack/rack/blob/master/lib/rack/session/cookie.rb#L139):

~~~
session_data = "session--data--digest"

digest, session_data = session_data.reverse.split("--", 2)
digest.reverse!        if digest
session_data.reverse!  if session_data

session_data  # => "session--data"
digest        # => "digest"
~~~

Note that each substring needs to be reversed back (for higher limits this would probably be done using #map), which seems inefficient and unhandy.

With rsplit this would become:

~~~
session_data = "session--data--digest"

session_data, digest = session_data.rsplit("--", 2)
~~~

----------------------------------------
Feature #9116: String#rsplit missing
https://bugs.ruby-lang.org/issues/9116#change-62714

* Author: Ramkumar Ramachandra
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
There's nothing corresponding to Python's rsplit(). A quick glance at rb_str_split_m() tells me that it should be pretty trivial to implement. Is there any specific reason it hasn't already been done?



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>