Why does this expression:
puts "abc" * 5
=> "abcabcabcabcabc"
not equal this expression?
5.times do puts "abc"
abc
abc
abc
abc
abc
=> 5
Could you please explain why they don't print the same result?
The first writes the string "abc" concatenated to itself five times:
"abc"*5 = "abc"+"abc"+"abc"+"abc"+"abc" = "abcabcabcabcabc"
The second piece of code writes "abc" using the puts function 5 times. The puts function writes a newline character after each message, meaning that it writes "abc\n" 5 times.
5.times do puts "abc"
turns to
puts "abc" ->also jumps to the next line
puts "abc" ->also jumps to the next line
puts "abc" ->also jumps to the next line
puts "abc" ->also jumps to the next line
puts "abc" ->also jumps to the next line
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments