Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...