📜  红宝石 |可枚举的 min_by()函数

📅  最后修改于: 2022-05-13 01:54:39.178000             🧑  作者: Mango

红宝石 |可枚举的 min_by()函数

enumerablemin_by()是 Ruby 中的一个内置方法,它返回一个满足给定块条件的最小元素数组。当没有给出块时,它返回一个枚举器。

示例 #1

# Ruby program for min_by method in Enumerable
  
# Initialize 
a = ["gopal", "tunday", "geeks", "classes", "linux"]
  
# Prints
p a.min_by(2) {|obj| obj.length }
  
p a.min_by {|obj| obj.length }

输出

["geeks", "gopal"]
"gopal"

示例 #2

# Ruby program for min_by method in Enumerable
  
# Initialize 
a = ["gopal", "tunday", "geeks", "classes", "linux"]
  
# Prints
p a.min_by(2) 
  
p a.min_by 

输出

Enumerator: ["gopal", "tunday", "geeks", "classes", "linux"]:min_by(2)
Enumerator: ["gopal", "tunday", "geeks", "classes", "linux"]:min_by