Daniel Finnie
1/14/2007 10:42:00 PM
I think the standard idiom is to use the min/max functions of an array:
a = 5
b = 10
[a, b].max #=> 10
You can also give max a block, similar to sort:
a = "Hello"
b = "Hi"
[a, b].max {|x, y| x.length <=> y.length}
Or you can write a method so it works more like sort_by (the interface,
not the implementation):
class Array
def max_by &blk
max {|a, b| blk.call(a) <=> blk.call(b)}
end
end
a = "Hello"
b = "Hi"
[a, b].max {|x| x.length}
And then, in Ruby 1.9, you should be able to do this (using max_by from
above):
a = "Hello"
b = "Hi"
[a, b].max(&:length) # Not sure if the syntax is 100%
And if you still want your max() function:
def min(*args)
args.min
end
Everything also applies to minimums using the min function.
Dan
Tom Ayerst wrote:
> Sorry for my beginners ruby (are there some standard min(x,y)/max(x,y,)
> functions?