Skip to Content
Ruby in a Nutshell
book

Ruby in a Nutshell

by Yukihiro Matsumoto
November 2001
Intermediate to advanced
218 pages
6h 22m
English
O'Reilly Media, Inc.
Content preview from Ruby in a Nutshell

Name

String — Character String class

Synopsis

String is one of Ruby’s basic datatypes, which contain arbitrary sequences of bytes. String can contain \0.

Included Module

Enumerable, Comparable

Class Method

String::new(str)

Creates a string.

Instance Methods

Methods of the String class ending in ! modify their receiver and return a string if modification took place, otherwise nil. Methods without a ! return a modified copy of the string.

~ s

Attempts to match pattern s against the $_ variable. This method is obsolete.

s % arg

An abbreviated form of sprintf(s, arg...). Multiple elements are specified using an array.

s * n

Returns a string consisting of s copied end to end n times.

s + str

Returns a string with str concatenated to s.

s << str

Concatenates str to s.

s =~ x

Performs a regular expression match. If x is a string, it’s turned into a Regexp object.

s[n]

Returns the code of the character at position n. If n is negative, it’s counted as an offset from the end of the string.

s[n..m]
s[n, len]

Returns a partial string.

"bar"[1..2]   # => "ar"
"bar"[1..-1]  # => "ar"
"bar"[-2..2]  # => "ar"
"bar"[-2..-1] # => "ar"
"bar"[1,2]    # => "ar"
"bar"[-1, 1]  # => "r"
s[n]=value

Replaces the n th element in the string with value. value may be a character code or string.

s[n..m]=str
s[n, len]=str

Replaces a part of the string with str.

s.capitalize
s.capitalize!

Returns a copy of s with the first character converted to uppercase and the remainder to lowercase.

"fooBar".capitalize            # => "Foobar"
s.center(w)

Returns a ...

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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Ruby by Example

Ruby by Example

Kevin C. Baird
Ruby Wizardry

Ruby Wizardry

Eric Weinstein
Ruby Best Practices

Ruby Best Practices

Gregory T Brown
Refactoring in Ruby

Refactoring in Ruby

William C. Wake, Kevin Rutherford

Publisher Resources

ISBN: 0596002149Errata Page