March 2009
Intermediate to advanced
194 pages
4h
English
At this point you may have tried some things that didn’t work. If not, here are a few:
puts '12' + 12 |
puts '2' * '5' |
example.rb:1:in `+': no implicit conversion of Fixnum into String (TypeError) |
from example.rb:1:in `<main>' |
Hmmm…an error message. The problem is that you can’t really add a number to a string or multiply a string by another string. It doesn’t make any more sense than this does:
puts 'Betty' + 12 |
puts 'Fred' * 'John' |
Here’s something else to be aware of: you can write 'pig'*5 in a program, since it just means five sets of the string 'pig' all added together. However, you can’t write 5*'pig', since that means 'pig' sets of the number 5, which is…poetic, at best.
Finally, what if we want a program to print ...