ruby max 2 numbers
To find the maximum of two numbers in Ruby, you can use the max
method. Here are the steps to do so:
Start by defining two variables, let's say
num1
andnum2
, and assign them the values of the numbers you want to compare.Use the
max
method to compare the two numbers. This method returns the maximum of the two values.Assign the result of the
max
method to a new variable, let's call itmaximum
.Finally, you can print the value of
maximum
to output the maximum of the two numbers.
Here is an example code snippet that demonstrates these steps:
num1 = 10
num2 = 20
maximum = [num1, num2].max
puts maximum
In this example, num1
is assigned the value of 10, and num2
is assigned the value of 20. The max
method is then used to compare these two values, and the result is stored in the maximum
variable. Finally, the value of maximum
is printed, which in this case would be 20.