Ruby min和max如何比较字符串?

时间:2022-09-28 14:05:55

I'm playing around with iterators. What is Ruby comparing using max and min?

我正在玩迭代器。什么是使用max和min进行Ruby比较?

If I have this array:

如果我有这个数组:

word_array = ["hi", "bob", "how's", "it", "going"]

and I run:

然后我跑:

puts word_array.max
puts word_array.min

I expect to get "going" or "how's" for max, since they're both five characters long, or "going" on the theory that it's the last item in the array. I expect min to return "hi" or "it", since they're tied for the shortest string. Instead, I get back:

我希望得到最大的“走”或“怎么样”,因为它们都是五个字符长,或“理论上”它是阵列中的最后一个项目。我希望min能够返回“hi”或“it”,因为它们是最短的字符串。相反,我回来了:

puts word_array.max -> it
puts word_array.min -> bob

What is Ruby measuring to make this judgement? It selected the shortest string for max and a middle length string for min.

什么是Ruby测量来做出判断?它选择最短的字符串为max,中间长度的字符串为min。

3 个解决方案

#1


5  

Actually, you are (kind of) asking the wrong question. max and min are defined on Enumerable. They don't know anything about Strings.

实际上,你是(有点)提出错误的问题。 max和min在Enumerable上定义。他们对字符串一无所知。

They use the <=> combined comparison operator (aka "spaceship"). (Note: pretty much any method that compares two objects will do so using the <=> operator. That's a general rule in Ruby that you can both rely on when using objects other people have written and that you should adhere to when writing your own objects: if your objects are going to be compared to one another, you should implement the <=> operator.)

他们使用<=>组合比较运算符(又名“太空飞船”)。 (注意:几乎任何比较两个对象的方法都是使用<=>运算符。这是Ruby中的一般规则,你可以在使用其他人编写的对象时依赖它,并且在编写自己的对象时应该遵守对象:如果要将对象相互比较,则应实现<=>运算符。)

So, the question you should be asking is, how does String#<=> compare Strings? Unfortunately, the answer is not quite clear from the documentation. However, it does mention that the shorter string is considered to be less than the longer string if the two strings are equal up to that point. So, it is clear that length is used only as a tie-breaker, not as the primary criterion (as you are assuming in your question).

所以,你应该问的问题是,String#<=>如何比较字符串?不幸的是,答案在文档中并不十分清楚。但是,它确实提到如果两个字符串相等于该点,则认为较短的字符串小于较长的字符串。因此,很明显长度仅用作打破平局,而不是主要标准(正如您在问题中所假设的那样)。

And really, lexicographic ordering is just the most natural thing to do. If I gave you a list of words and asked you to order them without giving you any criteria, would you order them by length or alphabetically? (Note, however, that this means that '20' is less than '3'.)

实际上,词典排序是最自然的事情。如果我给你一个单词列表并要求你在不给你任何标准的情况下订购它们,你会按照长度或字母顺序排序吗? (但请注意,这意味着'20'小于'3'。)

#2


3  

I believe it's doing a lexical sort, exactly like a dictionary order. The maximum would be the furthest one in the dictionary.

我相信它正在进行词汇排序,就像字典顺序一样。最大值将是字典中最远的一个。

#3


0  

http://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-max_by

ar = ['one','two','three','four','five']
ar.max_by(&:length) # => "three"

#1


5  

Actually, you are (kind of) asking the wrong question. max and min are defined on Enumerable. They don't know anything about Strings.

实际上,你是(有点)提出错误的问题。 max和min在Enumerable上定义。他们对字符串一无所知。

They use the <=> combined comparison operator (aka "spaceship"). (Note: pretty much any method that compares two objects will do so using the <=> operator. That's a general rule in Ruby that you can both rely on when using objects other people have written and that you should adhere to when writing your own objects: if your objects are going to be compared to one another, you should implement the <=> operator.)

他们使用<=>组合比较运算符(又名“太空飞船”)。 (注意:几乎任何比较两个对象的方法都是使用<=>运算符。这是Ruby中的一般规则,你可以在使用其他人编写的对象时依赖它,并且在编写自己的对象时应该遵守对象:如果要将对象相互比较,则应实现<=>运算符。)

So, the question you should be asking is, how does String#<=> compare Strings? Unfortunately, the answer is not quite clear from the documentation. However, it does mention that the shorter string is considered to be less than the longer string if the two strings are equal up to that point. So, it is clear that length is used only as a tie-breaker, not as the primary criterion (as you are assuming in your question).

所以,你应该问的问题是,String#<=>如何比较字符串?不幸的是,答案在文档中并不十分清楚。但是,它确实提到如果两个字符串相等于该点,则认为较短的字符串小于较长的字符串。因此,很明显长度仅用作打破平局,而不是主要标准(正如您在问题中所假设的那样)。

And really, lexicographic ordering is just the most natural thing to do. If I gave you a list of words and asked you to order them without giving you any criteria, would you order them by length or alphabetically? (Note, however, that this means that '20' is less than '3'.)

实际上,词典排序是最自然的事情。如果我给你一个单词列表并要求你在不给你任何标准的情况下订购它们,你会按照长度或字母顺序排序吗? (但请注意,这意味着'20'小于'3'。)

#2


3  

I believe it's doing a lexical sort, exactly like a dictionary order. The maximum would be the furthest one in the dictionary.

我相信它正在进行词汇排序,就像字典顺序一样。最大值将是字典中最远的一个。

#3


0  

http://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-max_by

ar = ['one','two','three','four','five']
ar.max_by(&:length) # => "three"