How to print output in Ruby?
There are various methods to print output. Most common methods to print output in ruby are print, puts and p.
Method 1. Using `puts` and `print`
By utilizing the `puts` and `print` methods, Ruby programmers commonly print output. These two methods serve to exhibit information on the console; however, their dissimilarity lies in newline management.
Each invocation of the ‘puts’ method initiates a fresh line, i.e. it introduces a newline character, thus, following the output.
puts "Hello Geeks"
puts "Welcome to Ruby programming."
Output:
Hello Geeks
Welcome to Ruby programming.
Unlike the ‘puts’ method, which automatically appends a newline character when consecutively called, the ‘print‘ method – lacking this automatic feature, it continues to print on the same line. To introduce line breaks between each call, one must expressly add a newline character (‘\n’).
print "Hello, "
print "world!\n"
print "Welcome to Ruby programming."
Output:
Hello, world!
Welcome to Ruby programming.
Method 2. Using `p`
The ‘p’ method displays an object’s raw value. It is mostly used for debugging purposes because it displays the object’s exact value, including any quotes or escape letters.
name = "Alice"
age = 30
array = [1, 2, 3]
# Using p method to print the values
p name
p age
p array
Output:
"Alice"
30
[1, 2, 3]
Method 3. Using `printf` for Formatted Output
If you need to customize your output with specific variable placeholders, it’s advisable to use the ‘printf’ method. This approach allows you a greater control over dictating and modifying your output’s format by employing carefully selected format specifiers.
name = "Alice"
age = 30
printf "Name: %s, Age: %d\n", name, age
Output:
Name: Alice, Age: 30
In the `printf` method:
- `%s` is a placeholder for a string.
- `%d` is a placeholder for an integer.
As needed, employ supplementary format specifiers: for example, use `%f` to signify floating-point numbers; utilize `%x` to indicate hexadecimal values—and continue accordingly.
Method 4. Interpolating Variables in Strings
Utilize the `#{}` syntax: This syntax allows you to insert the value of a variable directly into a string, making it more readable and easier to construct dynamic strings.
name = "Bob"
age = 25
puts "Name: #{name}, Age: #{age}"
Ouput:
Name: Bob, Age: 25
Interpolation is the process of embedding variable values or expressions directly into strings. Through this technique, readability and cleanness in code are significantly enhanced.
Method 5. Using Here Documents for Multiline Output
If you require the printing of multiline text or preservation of a block’s formatting, employ Here Document (`<<`) in Ruby.
message = <<~MSG
This is a multiline
message.
It preserves formatting.
MSG
puts message
Output:
This is a multiline
message.
It preserves formatting.
When your code involves handling lengthy strings or text blocks, Here Documents emerge as notably advantageous.
How to print output in Ruby?
An essential aspect of programming involves the ability to print output, This allows developers to not only communicate information with users but also debug code and present results. Within Ruby, a potent and adaptable language for programming, numerous methods exist for printing output directly into the console.