Note: Difference between Ruby "p" and "puts"

2020/05/17

When I was doing competitive programming with Atcoder Keep a memorandum of the mistakes you made.

Atcoder168_B

OK if the following is displayed in the output

$ ruby Bmain.rb 
4 #input
aaaaa #input
aaaa... #output

I wrote the following code.


K = gets.to_i
S = gets.chomp

if S.length <= K then
  p S
elsif S.length > K then
  p S[0..(K-1)]+"..."
end

output An error occurs because "" is displayed in the output.

$ ruby Bmain.rb 
4 #input
aaaaa #input
"aaaa..." #output

Change p to puts

K = gets.to_i
S = gets.chomp


if S.length <= K then
  puts S
elsif S.length > K then
  puts S[0..(K-1)]+"..."
end

The following was displayed without "".

$ ruby Bmain.rb 
4 #input
aaaaa #input
aaaa... #output

Since p is for debugging purposes, "" is added for easy understanding.

Recommended Posts

Note: Difference between Ruby "p" and "puts"
[Ruby] Difference between print, puts and p
Difference between puts and print
[Ruby] Difference between puts and return, output and return value
[Ruby] Difference between get and post
[Ruby] Difference between is_a? And instance_of?
Difference between Ruby instance variable and local variable
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between vh and%
Difference between i ++ and ++ i
[Ruby] I thought about the difference between each_with_index and each.with_index
Difference between product and variant
Difference between redirect_to and render
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between redirect_to and render
About the difference between classes and instances in Ruby
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
[Ruby] Difference between match / scan
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
The difference between programming with Ruby classes and programming without it
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
[Ruby] Maybe you don't really understand? [Difference between class and module]
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Easy to understand the difference between Ruby instance method and class method.
Difference between final and Immutable in Java