Skip to Main Content
Learn to Program, 2nd Edition
book

Learn to Program, 2nd Edition

by Chris Pine
March 2009
Intermediate to advanced content levelIntermediate to advanced
194 pages
4h
English
Pragmatic Bookshelf
Content preview from Learn to Program, 2nd Edition

Problems

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn to Program, 3rd Edition

Learn to Program, 3rd Edition

Chris Pine
The Book of Ruby

The Book of Ruby

Huw Collingbourne

Publisher Resources

ISBN: 9781680500172Errata