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

Array — Array class

Synopsis

Array is a class for an ordered collection of objects, indexed by integer. Any kind of object may be stored in an Array. Arrays grow as you add elements.

Included Module

Enumerable

Class Methods

Array[x...]

Creates an array.

Array::new([size=0[, fill=nil]])

Creates an array. Its size and initial values may also be specified.

Array::new(4, "foo")      # => ["foo","foo","foo","foo"]

Instance Methods

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

arr & array

Returns an array of elements common to both arrays.

[1,3,5]|[1,2,3]           # => [1,3]
[1,3,5]|[2,4,6]           # => [1,2,3,4,5,6]
arr| array

Returns an array combining elements from both arrays.

[1,3,5]|[2,4,6]           # => [1,2,3,4,5,6]
arr * n

If n is an integer, returns a copy of array with n copies of arr concatenated to it. If n is a string, the equivalent of arr.join(n) is performed.

[5] * 3                   # => [5, 5, 5].
["foo", "bar"] * "-"      # => "foo-bar"
arr + array

Returns a copy of arr with array concatenated to its end.

arr - array

Returns a new array that is a copy of arr, removing any items in array.

[1, 2, 3, 4] - [2, 3]     # => [1, 4]
arr << item

Appends item to arr.

arr[n]

References the nth element of arr. If n is negative, it’s interpreted as an offset from the end of arr.

arr[n..m]
arr[n,len]

Returns a partial string.

arr[n]=item
arr[r..m]=array
arr[r,len]=array

Assigns item or arr to the specified elements.

arr = [0, ...
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