如何用Ruby找到最小/最大值?

时间:2022-06-01 20:59:52

I want to do something simple and straightforward, like min(5,10), or Math.max(4,7). Are there functions to this effect in Ruby?

我想做一些简单明了的事情,比如min(5,10)或Math.max(4,7)。在Ruby中有这样的功能吗?

5 个解决方案

#1


590  

You can do

你可以做

[5, 10].min

or

[4, 7].max

They come from the Enumerable module, so anything that includes Enumerable will have those methods available.

它们来自可枚举模块,因此包含可枚举的任何内容都可以使用这些方法。

EDIT

编辑

@nicholasklick mentions another option, Enumerable#minmax, but this time returning an array of [min, max].

@nicholasklick提到了另一个选项,可枚举的#minmax,但这次返回的是[min, max]的数组。

[4, 7].minmax
=> [4, 7]

It doesn't seem very interesting with only 2 values in the initial array, so

在初始数组中只有2个值看起来不太有趣。

[4, 5, 7, 10].minmax
=> [4, 10]

#2


46  

You can use

您可以使用

[5,10].min 

or

[4,7].max

It's a method for Arrays.

它是数组的一种方法。

#3


19  

All those results generate garbage in a zealous attempt to handle more than two arguments. I'd be curious to see how they perform compared to good 'ol:

所有这些结果都是为了处理两个以上的参数而产生的垃圾。我很想知道他们的表现和好的“ol”相比:

def max (a,b)
  a>b ? a : b
end

which is by-the-way my official answer to your question. :)

这是我对你问题的官方回答。:)

#4


18  

In addition to the provided answers, if you want to convert Enumerable#max into a max method that can call a variable number or arguments, like in some other programming languages, you could write:

除了提供的答案之外,如果您想将可枚举的#max转换为一个可以调用变量数或参数的max方法,比如在其他一些编程语言中,您可以这样写:

def max(*values)
 values.max
end

Output:

输出:

max(7, 1234, 9, -78, 156)
=> 1234

This abuses the properties of the splat operator to create an array object containing all the arguments provided, or an empty array object if no arguments were provided. In the latter case, the method will return nil, since calling Enumerable#max on an empty array object returns nil.

这滥用了splat操作符的属性来创建包含所有参数的数组对象,或者如果没有提供参数,则创建一个空数组对象。在后一种情况下,该方法将返回nil,因为在空数组对象上调用Enumerable#max返回nil。

If you want to define this method on the Math module, this should do the trick:

如果你想在数学模块上定义这个方法,这应该可以做到:

module Math
 def self.max(*values)
  values.max
 end
end

Note that Enumerable.max is, at least, two times slower compared to the ternary operator (?:). See Dave Morse's answer for a simpler and faster method.

注意,可点数的。与三元算符相比,max至少要慢2倍。请参见Dave Morse对更简单、更快速的方法的回答。

#5


13  

If you need to find the max/min of a hash, you can use #max_by or #min_by

如果您需要找到散列的最大/最小值,可以使用#max_by或#min_by。

people = {'joe' => 21, 'bill' => 35, 'sally' => 24}

people.min_by { |name, age| age } #=> ["joe", 21]
people.max_by { |name, age| age } #=> ["bill", 35]

#1


590  

You can do

你可以做

[5, 10].min

or

[4, 7].max

They come from the Enumerable module, so anything that includes Enumerable will have those methods available.

它们来自可枚举模块,因此包含可枚举的任何内容都可以使用这些方法。

EDIT

编辑

@nicholasklick mentions another option, Enumerable#minmax, but this time returning an array of [min, max].

@nicholasklick提到了另一个选项,可枚举的#minmax,但这次返回的是[min, max]的数组。

[4, 7].minmax
=> [4, 7]

It doesn't seem very interesting with only 2 values in the initial array, so

在初始数组中只有2个值看起来不太有趣。

[4, 5, 7, 10].minmax
=> [4, 10]

#2


46  

You can use

您可以使用

[5,10].min 

or

[4,7].max

It's a method for Arrays.

它是数组的一种方法。

#3


19  

All those results generate garbage in a zealous attempt to handle more than two arguments. I'd be curious to see how they perform compared to good 'ol:

所有这些结果都是为了处理两个以上的参数而产生的垃圾。我很想知道他们的表现和好的“ol”相比:

def max (a,b)
  a>b ? a : b
end

which is by-the-way my official answer to your question. :)

这是我对你问题的官方回答。:)

#4


18  

In addition to the provided answers, if you want to convert Enumerable#max into a max method that can call a variable number or arguments, like in some other programming languages, you could write:

除了提供的答案之外,如果您想将可枚举的#max转换为一个可以调用变量数或参数的max方法,比如在其他一些编程语言中,您可以这样写:

def max(*values)
 values.max
end

Output:

输出:

max(7, 1234, 9, -78, 156)
=> 1234

This abuses the properties of the splat operator to create an array object containing all the arguments provided, or an empty array object if no arguments were provided. In the latter case, the method will return nil, since calling Enumerable#max on an empty array object returns nil.

这滥用了splat操作符的属性来创建包含所有参数的数组对象,或者如果没有提供参数,则创建一个空数组对象。在后一种情况下,该方法将返回nil,因为在空数组对象上调用Enumerable#max返回nil。

If you want to define this method on the Math module, this should do the trick:

如果你想在数学模块上定义这个方法,这应该可以做到:

module Math
 def self.max(*values)
  values.max
 end
end

Note that Enumerable.max is, at least, two times slower compared to the ternary operator (?:). See Dave Morse's answer for a simpler and faster method.

注意,可点数的。与三元算符相比,max至少要慢2倍。请参见Dave Morse对更简单、更快速的方法的回答。

#5


13  

If you need to find the max/min of a hash, you can use #max_by or #min_by

如果您需要找到散列的最大/最小值,可以使用#max_by或#min_by。

people = {'joe' => 21, 'bill' => 35, 'sally' => 24}

people.min_by { |name, age| age } #=> ["joe", 21]
people.max_by { |name, age| age } #=> ["bill", 35]