ResourcefulDB
asked on
Quick question on puts, p, and print in Ruby
why are those three commands different?
In ruby, we use puts, p, and print to print a line to screen to ask a question. Would someone please explain it to me the difference in the following situation?
if we do the following:
print "Input your string."
str = gets.chomp
or the following:
p "input your string."
str = gets.chomp
the screen will have the question printed out; then we can input a string.
However, if we do the following in the script:
puts "input your string."
str = gets.chomp
When we run it, the screen will not show the question. But, after we type something and hit a return, the question will pop out after we hit a return.
why does ruby work in that way?
thanks,
RDB
In ruby, we use puts, p, and print to print a line to screen to ask a question. Would someone please explain it to me the difference in the following situation?
if we do the following:
print "Input your string."
str = gets.chomp
or the following:
p "input your string."
str = gets.chomp
the screen will have the question printed out; then we can input a string.
However, if we do the following in the script:
puts "input your string."
str = gets.chomp
When we run it, the screen will not show the question. But, after we type something and hit a return, the question will pop out after we hit a return.
why does ruby work in that way?
thanks,
RDB
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.