0

Possible Duplicate:
More concise version of max/min without the block

If I had N number of objects with specific attributes (in this example height), what is a good way to find the max or min?

 class Person
   attr_accessor: height
 end

 a = Person.new
 a.height = 10
 b = Person.new
 b.height = 11
 c = Person.new
 c.height = 12

 #what's a nice way to get the tallest person
Community
  • 1
  • 1
probably at the beach
  • 14,489
  • 16
  • 75
  • 116

1 Answers1

5

To expand the answer here:

class Person
  attr_accessor :height

  def initialize(height)
    self.height = height
  end
end

people = [ Person.new(10), Person.new(20), Person.new(30) ]

tallest_person = people.max_by &:height
Community
  • 1
  • 1
cantlin
  • 3,236
  • 3
  • 21
  • 22
  • So basically what @cantlin is saying is to store them in an array and sort it. Which will probably be the answer to most any question that involves looking for the highest of something (unless you absolutely can't change their order) – KChaloux May 17 '12 at 12:19