In this tutorial, we will learn about the 'puts' method in Ruby. We will cover the basics of using 'puts' to output data to the console.
The 'puts' method in Ruby is used to print output to the console. It adds a newline after each argument.
The syntax to use 'puts' in Ruby is:
puts object
We can use puts method to print a string.
For example,
greeting
.puts
to print the value of the variable.greeting = 'Hello, World!'
# Print the value of the variable
puts greeting
Hello, World!
We can print a multiline string using puts method.
For example,
puts
to print the first line.puts
to print the second line.puts 'First line'
puts 'Second line'
First line Second line
We can print the elements of an array to output using puts method.
For example,
fruits
.puts
to print each element of the array.fruits = ['Apple', 'Banana', 'Cherry']
# Print each element of the array
puts fruits
Apple Banana Cherry