Ruby Containers, Iterators, and Blocks: CSE 413 Autumn 2008
Ruby Containers, Iterators, and Blocks: CSE 413 Autumn 2008
Ruby Containers,
Iterators, and Blocks
C t i
Containers
iin R
Ruby
b
Ruby has general, easy-to-use container
classes,, like most scripting
p g languages
g g
Two major kinds
Arrays:
ordered by position
Hashes: collections of <key, value> pairs
Often known as associative arrays
arrays, maps
maps, or
dictionaries
Unordered
R b A
Ruby
Arrays
R b H
Ruby
Hashes
h
pets[puff] pets[fido]
pets[cheeta]
p
[
] = monkey
y
(Can
C t i
Containers
and
d It
Iterators
t
Bl k
Blocks
A block is a sequence of statements
surrounded by { } or do end
Blocks must appear immediately following
the method call that executes them, on the
same line
Blocks may have 1 or more parameters at
the beginning surrounded by | |
Initialized
Bl k as Cl
Blocks
Closures
all_words
words.each { | w | all_words = all_words + w + }
These
M
More
Bl
Block
kU
Uses
Well