To print without a newline in Ruby, we simply need to use the print command instead of puts. Let’s take a look at each below.

Here is how the print command works.

number_one = 1
number_two = 2
number_three = 3
number_four = 4
number_five = 5

print number_one
print number_two
print number_three
print number_four
print number_five

#Output
12345

Let’s see how this example would work if we used the puts command.

number_one = 1
number_two = 2
number_three = 3
number_four = 4
number_five = 5

puts number_one
puts number_two
puts number_three
puts number_four
puts number_five

#Output
1
2
3
4
5

So as you can see, the print command will print without a newline after it, while the puts command will return a newline after it prints.

Hopefully this article has been helpful for you to learn how to use Ruby to print without newline.

Categorized in:

Ruby,

Last Update: March 11, 2024