Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

ruby - What is the opposite of string.next?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...