HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RubyVariablesData TypesOperatorsStringsArraysHash (Dictionary)Conditions (if / else)LoopsMethods (def)Classes
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
arr [ ]push / << / popinclude? / indexmapselect / rejectreduce / sumsort / reverse / each
🗂️ Hash (Dictionary)
🔁 Condition & Loop
🧩 Method & Class
Ruby›REFERENCE›Array
📋

Array

Ordered data and powerful array methods.

CommandDescription
arr [ ]A collection of values.
push / << / popAdds / removes elements.
include? / indexIs the value in the array?
mapTransforms each element and produces a new arr.
select / rejectSelects elements matching a condition.
reduce / sumReduces the array to a single value.
sort / reverse / eachSorting and iteration.

arr [ ]

Try it yourself →

A collection of values.

Syntax

name = [e1, e2, ...]

Description

Array with square brackets. Elements are accessed with 0-based indexing; -1 is the last element.

Example

colors = ["red", "purple", "green"]
puts colors[0]
puts colors[-1]
puts colors.length

Expected output

red
green
3

When to use?

Storing lists.

push / << / pop

Try it yourself →

Adds / removes elements.

Syntax

arr << element  ·  arr.push(e)

Description

<< and push add to the end; pop removes and returns the last element.

Example

a = [1, 2]
a << 3
p a
puts a.pop

Expected output

[1, 2, 3]
3

When to use?

Growing lists, stack.

include? / index

Try it yourself →

Is the value in the array?

Syntax

arr.include?(value)

Description

include? returns true/false; index returns the position.

Example

a = ["x", "y", "z"]
puts a.include?("y")

Expected output

true

When to use?

Membership check.

map

Try it yourself →

Transforms each element and produces a new arr.

Syntax

arr.map { |e| newValue }

Description

Applies the block operation to each element; returns a new arr. Same as collect.

Example

p [1, 2, 3].map { |x| x * 2 }

Expected output

[2, 4, 6]

When to use?

Transforming the list.

select / reject

Try it yourself →

Selects elements matching a condition.

Syntax

arr.select { |e| condition }

Description

select takes those that satisfy the condition, reject takes those that do not. filter is the same as select.

Example

p [1, 2, 3, 4].select { |x| x.even? }

Expected output

[2, 4]

When to use?

Filtering the list.

reduce / sum

Try it yourself →

Reduces the array to a single value.

Syntax

arr.reduce(0) { |t, e| t + e }

Description

Accumulates elements into a single result. Shortcut for sum: arr.sum.

Example

puts [1, 2, 3, 4].reduce(0) { |t, x| t + x }
puts [1, 2, 3, 4].sum

Expected output

10
10

When to use?

Sum, product, concatenation.

sort / reverse / each

Try it yourself →

Sorting and iteration.

Syntax

arr.sort · arr.each { |e| ... }

Description

sort sorts, reverse reverses (new arr). each runs a block for each element.

Example

p [3, 1, 2].sort
["al", "mor"].each { |r| puts r }

Expected output

[1, 2, 3]
al
mor

When to use?

Sorting, traversing lists.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code