概要
Enumerable#max
詳細
Enumerable#max <=>メソッドを利用して最大値を返します。
ブロックを指定した場合は、ブロックの評価結果で判定を行います。
サンプル
コード
# encoding: utf-8 class Person include Enumerable attr_accessor :name, :age def initialize(name, age) @name, @age = name, age end def <=>(other) self.name <=> other.name end end persons = [Person.new("tanaka", 20), Person.new("sato", 55), Person.new("suzuki", 77)] p persons p persons.max p persons.max {|one, other|one.age <=> other.age}
出力
[#<Person:0x23df618 @name="tanaka", @age=20>, #<Person:0x23df4b0 @name="sato", @age=55>, #<Person:0x23df3d8 @name="suzuki", @age=77>] #<Person:0x23df618 @name="tanaka", @age=20> #<Person:0x23df3d8 @name="suzuki", @age=77>