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

The chomp Method

Excitement! Now we can make interactive programs! In this one, type your name, and it will greet you:

puts ​'Hello there, and what​\'​s your name?'
name = gets
puts ​'Your name is '​ + name + ​'? What a lovely name!'
puts ​'Pleased to meet you, '​ + name + ​'. :)'

Eek! I just ran it—I typed my name, and this is what happened:

<= Hello there, and what's your name?
=> Chris
<= Your name is Chris
 ? What a lovely name!
 Pleased to meet you, Chris
 . :)

Hmmm...it looks like when I typed the letters C, h, r, i, and s and then pressed Enter, gets got all the letters in my name and the Enter! Fortunately, there’s a method that deals with just this sort of thing: chomp. It takes off any Enter characters hanging out at the end of your string. ...

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