Home »
Ruby programming »
Ruby programs
Ruby program to sort an array with command line arguments
Sorting an array in Ruby: Here, we are going to learn how to sort an array with command line arguments in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on September 02, 2019
Sorting an array in Ruby
In this program, we are introducing the concept of command line arguments. Command line arguments can be taken from the users with the help of 'ARGV' keyword. Command line arguments are nothing but the input provided to the program code with the help of command line. We have to sort those command line arguments with the help of any method and provide the sorted strings as the output of the code.
Methods used:
- .length: This method is used to find the length of the object. Here, we are employing it to find the number of arguments provided by the user.
- puts: This is a very common yet important method. It is used to put strings as message on the console. here we are printing elements of array with the help of this method.
Variables used:
- str: This container is used to store the command line arguments.
- ch: It is used to store the temporary string. It is employed during sorting.
- l: It is storing the value being returned from the str.length function.
- i: It is a loop variable.
- j: It is also a loop variable used for sorting.
Ruby code to sort an array with command line arguments
=begin
Ruby program to sort an array with command line arguments
=end
str = ARGV #taking input from command line
l = str.length
ch = "" #temporary string
for i in 1..l
for j in 0..l-1
#comparison of strings
if(str[j].to_s>str[j+1].to_s)
ch=str[j]
str[j] = str[j+1]
str[j+1]=ch
end
end
end
puts str
Output
Run 1:
Amisha Satyam Ayush Saksham Nikhil
Amisha
Ayush
Hrithik
Nikhil
Saksham
Satyam
Run 2:
Orange Banana Pineapple Papaya
Apple
Banana
Orange
Papaya
Pineapple
Code logic:
In the above code, we are taking strings as input from the command line. These strings are referred to as command-line arguments. We have declared a temporary string named ch. We are applying a very common sorting method in which two loops are employed. Strings are compared and whatever the result comes out, further steps are implemented accordingly.