Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- What is the difference between:
- "h" + "a" * 10
- and
- ("h" + "a") * 10
- and why are they different?
- "h" + "a" * 10 results in "haaaaaaaaaa"
- It concatonates the "h" and the "a" once, while adding the "a" ten times, as you've instructed.
- ("ha" + "a") * 10 results in "hahahahahahahahahaha"
- It concatonates the "h" and the "a" into a string ten times, since the parentheses indicate the * 10 should apply to everything within.
- Interestingly, while experimenting with this, I learned that in order for multiplication to work on a string, you have to start your line of code with a string. If you start with a fixnum, it will result in an error. However, if you start with a string, you can add any amount of math behind it, and it will work. For example, 10 * 10 * ("I will not" + " swallow my Ruby.") results in an error, but ("I will not" + " swallow my Ruby.") * 10 * 10 correctly displays the entire string 100 times.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement