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

Stringy Superpowers

I’d really feel like I was doing you a disservice if I didn’t show you at least a little more of what strings can do (in Ruby, at least). Plus, if I do, I can give you more interesting exercises. Mind you, I’m still not going to show you even half, but I’ve just got to show you a little more.

Remember back here when I said a lot of the string methods also work on arrays? Well, it goes both ways: some of the array methods you’ve learned also work on strings.

Perhaps the most important and versatile is the […] method. The first thing you can do with it is pass in a number and get the character at that position in the string:

da_man = ​'Mr. T'
big_T = da_man[4]
puts big_T
T

And then you can do fun stuff like this:

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