find the largest element in an array in ruby

To find the largest element in an array in Ruby, you can use the max method. The max method returns the maximum value in an array. Here's an example:

main.rb
arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

largest = arr.max

puts largest
73 chars
6 lines

Output:

main.rb
9
2 chars
2 lines

In this example, we define an array arr containing some integers. We then call the max method on the array to find the largest element, and store the result in a variable called largest. Finally, we print out the value of largest.

related categories

gistlibby LogSnag