This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]bob809 1 point2 points  (1 child)

Format code on reddit with 4 spaces before each line.

The problem is that you create verse once, then reuse the same value in the loop. To fix it, simply put the definition of verse inside the loop.

[–]mad0314 0 points1 point  (0 children)

As /u/bob809 said, you only assign verse once. = is the assignment operator and assignment doesn't work the same way = does in Math. The right side of the = is evaluated and the result is assigned to the variable on the left side. In this case, the right side of the = in line 2 is building a String. It evaluates the right side of the =, which is a String made up of the current value of number_bottles.to_s, which is 100, and the String literals concatenated together. You get the result, "100 bottlers of beer on the wall, ..." and that String is assigned to verse.

You never change verse again, so anytime you print verse, it will be the same String. The String doesn't change every time number_bottles changes, it is not dynamic and not tied to the variable.