Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant ruby syntax to return the greater of two objects

Of course there are a thousand ways to get this done, but is the simplest (or most elegant) way to achieve this?

[4,8].max 

That's actually not too shabby, but what would you do?

like image 960
doctororange Avatar asked Mar 13 '10 15:03

doctororange


People also ask

What does <=> mean in Ruby?

It's a general comparison operator. It returns either a -1, 0, or +1 depending on whether its receiver is less than, equal to, or greater than its argument.

What does :: In Ruby mean?

The use of :: on the class name means that it is an absolute, top-level class; it will use the top-level class even if there is also a TwelveDaysSong class defined in whatever the current module is.


1 Answers

If you don't want to spawn an array, there's the conditional operator:

max = a > b ? a : b 
like image 119
FMc Avatar answered Sep 21 '22 19:09

FMc