[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

array of integers

Brad Tilley

10/31/2006 1:16:00 PM

What is the best way to get determine the largest and smallest int in an
array of ints? For example:

x = [1,2,3,4]

x.smallest should = 1
x.largest should = 4

How is this done in Ruby?

Thank you,
Brad

--
Posted via http://www.ruby-....

2 Answers

Thomas Adam

10/31/2006 1:19:00 PM

0

On Tue, Oct 31, 2006 at 10:15:35PM +0900, Brad Tilley wrote:
> What is the best way to get determine the largest and smallest int in an
> array of ints? For example:
>
> x = [1,2,3,4]
>
> x.smallest should = 1
> x.largest should = 4
>
> How is this done in Ruby?

[n6tadam@workstation fvwm]% irb
>> [1,2,3].max
=> 3
>> [1,2,3].min
=> 1

-- Thomas Adam

--
"Wanting to feel; to know what is real. Living is a lie." -- Purpoise
Song, by The Monkees.

Mike Fletcher

10/31/2006 2:13:00 PM

0

Brad Tilley wrote:
> What is the best way to get determine the largest and smallest int in an
> array of ints? For example:
>
> x = [1,2,3,4]
>

puts x.max
puts x.min

See the documentation for Enumerable (try "ri Enumerable" at your
prompt).

--
Posted via http://www.ruby-....