FAQ: Introduction to Ruby - 'puts' and 'print'

This community-built FAQ covers the “‘puts’ and ‘print’” exercise from the lesson “Introduction to Ruby”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise ‘puts’ and ‘print’

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’ve seen some examples using p to print a statement or a value. Is there any major difference using p, print, and puts?

What is p useful for?

Debugging.

When you’re looking for things like (normally invisible) newline characters, or you want to make sure some value is correct, then you use p .

I am doing puts and print but when I try to do a new one, it comes in the same line of the print, why is that? and how can i add a new line

1 Like

because print() doesn’t put a new-line character (\n), you could add this character manually, but then its better to use puts

2 Likes

And for strings, is best practice " " in Ruby? The tutorials for JS used ’ ’ for strings and have switched to " " for Ruby.

I am totally new to coding. I want to know what I am supposed to do in the exercise. What should I type in the editor?

The exercise just wants to familiarize you with the difference between print and puts. You can use any strings you like. For Example, try entering the following:

puts "What's up?"
print "Hello!"
puts "Some more putting!!!"
print "Okay! Bye!!!!!!"

The output you will see is:

What’s up?
Hello!Some more putting!!!
Okay! Bye!!!

Reading the explanation in the exercise, do you understand the output?
Do you understand why some output is on a new line and some output is not?
Try experimenting with mutlple print and puts statements in different order until you have a strong grasp of the difference between print and puts.

1 Like

How well would puts handle tabular data to populate a row, given one item at a time?

my_array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
my_array.each { |num| puts num unless num % 2 != 0 }
puts my_array.select { |x| x % 2 === 0 }
puts my_array.select { |x| x.even? }
my_array.each { |num| puts num if num.even? }

Output

2
4
6
8
10
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10]
2
4
6
8
10

The lines that employ data structures printed as rows. The others, well, did not. We needed to employ a data structure to get a row to print, and even then, it was as an object, not tabular data.

Clearly we have evidence that an inline print is needed that keeps the draw pencil in its last place, without a newline added. print to the rescue. It keeps the draw pencil in its last position when finished executing.

Let’s go down a rabbit hole…

def list(x, sep='')
  x.split(sep)
end
puts list('every')
puts list('every good boy deserves favour', ' ')

Output

["e", "v", "e", "r", "y"]
["every", "good", "boy", "deserves", "favour"]
def keys
  list('every good boy deserves favour', ' ')
end
keys.each { |x| puts list(x).each { |c| print c } }

Output

every["e", "v", "e", "r", "y"]
good["g", "o", "o", "d"]
boy["b", "o", "y"]
deserves["d", "e", "s", "e", "r", "v", "e", "s"]
favour["f", "a", "v", "o", "u", "r"]

Give that one a minute to sink in.

Not to worry; there is a cure for that…

keys.each { |x| puts list(x).each { |c| print c } && nil }

Output

every
good
boy
deserves
favour

Every character above, including newline, was sent to standard output, one at a time.


Aside

I have used the name of a Moody Blues album so stayed true to their spelling (and grammar).


Further aside

Watching this code play out is possible in real time, given how slow print is, and how it hogs resources to the nines. Isolate out the list and keys objects and the keys.each... and run that code. Watch the console as the output ‘unfolds’. Cool. It wasn’t just the printing, but the nested ‘each’ that slowed things down.

Still, cool to watch.

1 Like