Dear readers, coders, and enthusiasts. Welcome to my next article about printing out data and reading input data from the user in Ruby programming language.
The main point you have to know to print out a string in Ruby, you have to enclose it in the singular (' ') or double quotes (" ").
To print anything on the screen you can use two functions. They are: “print” and “puts”.
- “print” — you can use its shorter version instead ("p").
- “puts” — comes from a combination of words “put” and “s” (string).
The main difference between these two functions is that the "print" function prints the required data and stays on the same line. If you use one more "print" to output data on the screen, it will be on the same line as a continuation of the former output.
# Printing multiple data one after another print "Hello" print "dear" print "friends!" # This is the output: # Hellodearfriends! # In this case, we can use a space (' ' or " ") to separate words. # we can print them separately on each line or # we can add an empty space after each of the words. print "Hello " print "dear " print "friends!"
“puts” function, compared to the former one, changes its position to the start of the new line after printing the required data. This way we get each output on a new line.
# Output each word on a new line... puts "Hello" puts "everyone" puts "around" puts "here..." # The result is, multiple words each on a new line : # Hello # everyone # around # here
Besides those two, we can take a look at the “putc” function.
- “putc” — combination of words “put” and “c” (character). prints a single character.
# 'putc' outputs a single line given as a parameter. putc "Hello" # We will see only 'H' character.
We generally use the function “gets” to read and input data from the user.
- “gets” — a combination of words “get” and “s” (string).
If we want to get any data from the user, we first have to read it as input via a keyboard or a mouse and then we can use it in our program.
Let's ask a user his name, read his name as input data, and greet him by his name.
# Ask the user to input his name. print "Please input your name: " # We read the input name and place it to 'user_name' variable user_name = gets # Now we can greet the user by his name puts "Hello " + user_name
In some cases, trying to print out any data that is not a string can lead to errors in Ruby programming language. In this case, we can convert this data to a string by using "to_s" function that converts any type of data to string data, and easily pass it to "print" or "puts" functions.
- “to_s” — a shortened version of “to string”. It converts any type of data to string type data.
# Output my age onto the screen. my_age = 18 # Sometimes this does not lead to error. # Because Ruby automatically converts it to a string. puts my_age # You can also do it this way. # Convert the number to a string value. This way it is even safer. puts my_age.to_s
You have fully covered ways to read data as an input and output program data to the screen in Ruby programming language. I truly hope this will help new learners of my beloved Ruby.
If you have anything to add please write it in the comments section and let me know about any mistakes or feedbacks as well.
Good Luck dear friends!