Why don't these string expressions print the same result?

Shawn

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?

user1849298

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex - Why don't these two expressions produce the same result?

From Dev

JS: Why don't the switch statement cases `case 2` and `case day == 2` print out the same result?

From Dev

Why don't these blocks of code give the same result?

From Dev

Why don't these blocks of code give the same result?

From Dev

Don't print the same string again if it was found between two files

From Dev

Drools : 2 identical calls to the same knowledgeBase don't give the same result... why?

From Dev

Why don't UTF-8 and UTF-16 encoded Strings print the same in Java?

From Dev

why print statement don't work as intended?

From Dev

Why don't the two pointer values be the same?

From Dev

Why don't these evaluate the same way?

From Dev

Why don't programming languages use simplified boolean expressions?

From Dev

Why don't lambda expressions require <functional>, but function<void()> does?

From Dev

Why don't subshell expressions in ~/.zshenv lead to infinite regress?

From Dev

Java - Why String( a).getBytes() == a not give the same result?

From Dev

Why string to hex give me same result

From Dev

Two strings print as same but don't pass equality test?

From Dev

Why don't all elements with the same class have the same padding?

From Dev

The getGeneratedKeys() to print key don't work. Why?

From Dev

Why OpenMP atomic and critical don't give the right result?

From Dev

why result of `charCodeAt(0)` and `e.keyCode` don't match?

From Dev

Why don't I get the result of this Metal kernel

From Dev

why result of `charCodeAt(0)` and `e.keyCode` don't match?

From Dev

Why OpenMP atomic and critical don't give the right result?

From Dev

Why don't the build result directories start ** in VS .gitignore

From Dev

Don't understand how to input/print and compare string in loop in C

From Dev

Why my code doesn't print any result?

From Dev

Why doesn't "print (string)" prints the string when the condition is "if '-' in string:"

From Dev

Why don't I get all 4 elements in the same row?

From Dev

Why don't these apt-packages behave the same on Ubuntu and Heroku?

Related Related

  1. 1

    Regex - Why don't these two expressions produce the same result?

  2. 2

    JS: Why don't the switch statement cases `case 2` and `case day == 2` print out the same result?

  3. 3

    Why don't these blocks of code give the same result?

  4. 4

    Why don't these blocks of code give the same result?

  5. 5

    Don't print the same string again if it was found between two files

  6. 6

    Drools : 2 identical calls to the same knowledgeBase don't give the same result... why?

  7. 7

    Why don't UTF-8 and UTF-16 encoded Strings print the same in Java?

  8. 8

    why print statement don't work as intended?

  9. 9

    Why don't the two pointer values be the same?

  10. 10

    Why don't these evaluate the same way?

  11. 11

    Why don't programming languages use simplified boolean expressions?

  12. 12

    Why don't lambda expressions require <functional>, but function<void()> does?

  13. 13

    Why don't subshell expressions in ~/.zshenv lead to infinite regress?

  14. 14

    Java - Why String( a).getBytes() == a not give the same result?

  15. 15

    Why string to hex give me same result

  16. 16

    Two strings print as same but don't pass equality test?

  17. 17

    Why don't all elements with the same class have the same padding?

  18. 18

    The getGeneratedKeys() to print key don't work. Why?

  19. 19

    Why OpenMP atomic and critical don't give the right result?

  20. 20

    why result of `charCodeAt(0)` and `e.keyCode` don't match?

  21. 21

    Why don't I get the result of this Metal kernel

  22. 22

    why result of `charCodeAt(0)` and `e.keyCode` don't match?

  23. 23

    Why OpenMP atomic and critical don't give the right result?

  24. 24

    Why don't the build result directories start ** in VS .gitignore

  25. 25

    Don't understand how to input/print and compare string in loop in C

  26. 26

    Why my code doesn't print any result?

  27. 27

    Why doesn't "print (string)" prints the string when the condition is "if '-' in string:"

  28. 28

    Why don't I get all 4 elements in the same row?

  29. 29

    Why don't these apt-packages behave the same on Ubuntu and Heroku?

HotTag

Archive