string.next 的反义词是什么? [英] What is the opposite of string.next?
问题描述
Ruby 中是否有与 String#succ
相反的方法?
Is there a method in Ruby that does the opposite String#succ
?
如果你运行 succ
或 next
你会得到这个:
If you run succ
or next
you get this:
a = "4.4.10"
a.succ
=> "4.4.11"
我想要相反的:
a = "4.4.10"
a.previous
=> "4.4.09"
这可能吗?我很难在 Ruby 文档中找到它.也许它不存在?
Is this possible? I was having a hard time finding this in the Ruby docs. Maybe it doesn't exist?
推荐答案
prev
或类似的东西不在标准 API 中,因为 succ
和一个假设的 prev
是满射.尽管如此,实现 Ruby 字符串类 Prev/Pred/Prev!/Pred! - 与 Next/Succ 方法相反" 是您可以使用的可能版本.
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.
根本问题在于succ
是不可逆的.这意味着,一旦你应用了 succ
,那么 prev
最终会产生一些歧义.
The root problem is that succ
is not inversible. This means, once you applied succ
, then prev
would end up with some ambiguity.
这篇关于string.next 的反义词是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!