-2

我是 Ruby 新手,想了解以下代码。

当我有一个带有字符串的数组,并使用 max 函数对其进行操作时:

names = ["aa", "bb", "cc"]
names.max = "cc" 

如果我在这里有一个哈希,

calendar["January", :cold, "February", :colder] 
calendar.max = ["January", :cold]

我无法理解它是最长的单词大小还是正在显示的索引大小最大的成员。谁能解释这种行为?我错过了一些非常基本的东西吗?

4

1 回答 1

4

数组通过比较对应位置的元素进行排序。字符串按字典顺序排序。"J"是“大于” "F",因此,["January", :cold]大于["February", :colder],不管字符串长度和剩余的数组元素。

months = %w[january february march april may june july august september october november december]

months.sort.join(', ') # => "april, august, december, february, january, july, june, march, may, november, october, september"
于 2013-02-04T10:33:25.580 回答