SameShirtEveryDay.com

Personal blog of the one called Alex Gorbatchev, from Toronto, Canada.

Where’s Math.min and Math.max in Ruby?

Posted on June 9th, 2007 by Alex Gorbatchev. In Ruby. 4 comments!

Maybe it’s just me and I don’t know where to look, but this isn’t something I’m interested in spending 30 minutes looking for.

module Math
  def self.max(a, b)
    a > b ? a : b
  end

  def self.min(a, b)
    a < b ? a : b
  end
end
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 comments.

  1. tanel

    but this wouldn’t be very object-oriented :)

    take a look at Enumerable#min, Enumerable#max

  2. Alex Gorbatchev

    Thanks for the tip!

    This means that I would have to create an array first.

    [1, 10].max

    To me this seems a lot less object oriented than having static methods. Creating a third object to compare two values seems quite a bit of an overkill and quite inefficient.

  3. I was also wondering where the hell it was :)
    Thanks.

    Nice blog too !

  4. Thanks!

    I hope these functions will make it to the official version of module Math some day…

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> , rel="nofollow" in use - no link dropping, no keywords or domains as names; do not spam, and do not advertise!

home
Subscribe to this blog Follow me on Twitter My bookmarks on Delicious My photography on Flickr