Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the opposite of string.next?

Tags:

string

ruby

Is there a method in Ruby that does the opposite String#succ?

If you run succ or next you get this:

a = "4.4.10"
a.succ
=> "4.4.11"

I want the opposite:

a = "4.4.10"
a.previous
=> "4.4.09"

Is this possible? I was having a hard time finding this in the Ruby docs. Maybe it doesn't exist?

like image 527
Sean Larkin Avatar asked May 23 '13 14:05

Sean Larkin


1 Answers

prev or something similar is not in the standard API because succ and a hypoethetical prev are surjective. Despite this, "Implement Ruby String Class Prev / Pred / Prev! / Pred! - Opposite Of Next / Succ Methods" is a possible version you could use.

The root problem is that succ is not inversible. This means, once you applied succ, then prev would end up with some ambiguity.

surjective map of succ and prev

like image 193
Bjoern Rennhak Avatar answered Oct 21 '22 21:10

Bjoern Rennhak