0% found this document useful (0 votes)
37 views

Ruby Containers, Iterators, and Blocks: CSE 413 Autumn 2008

Ruby has two main container types: arrays and hashes. Arrays are indexed by position and store ordered elements, while hashes store key-value pairs in an unordered manner. Both containers can iterate through their elements using the "each" method, which executes a block of code for each element. Blocks in Ruby are sequences of code surrounded by braces or do/end that are passed to methods. They can access variables in the surrounding scope, making them similar to closures. Blocks are commonly used for iteration but have many other uses as well.

Uploaded by

BradFanp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Ruby Containers, Iterators, and Blocks: CSE 413 Autumn 2008

Ruby has two main container types: arrays and hashes. Arrays are indexed by position and store ordered elements, while hashes store key-value pairs in an unordered manner. Both containers can iterate through their elements using the "each" method, which executes a block of code for each element. Blocks in Ruby are sequences of code surrounded by braces or do/end that are passed to methods. They can access variables in the surrounding scope, making them similar to closures. Blocks are commonly used for iteration but have many other uses as well.

Uploaded by

BradFanp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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

Instances of class Array


Create with an array literal, or Array.new

words = [ how, now, brown, cow ]


stuff = [ thing, 413, nil ]
seq = Array.new
Array new

Indexed with [ ] operator, 0-origin; negative


indices count from right
g

words[0] stuff[2] words[-2]


seq[1] = something

R b H
Ruby
Hashes
h

Instances of class Hash


Create with an hash literal, or Hash.new

pets = { spot => dog, puff => cat }


tbl = Array.new

Indexed with [ ] operator

pets[puff] pets[fido]
pets[cheeta]
p
[
] = monkey
y

(Can

use almost anything as key type; can use


anything as element type)

C t i
Containers
and
d It
Iterators
t

All containers respond to the message


each,, executing
g a block of code for each
item in the container
words.each { p
puts another word }
words.each { | w | puts w }

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

by the method that runs the block

Bl k as Cl
Blocks
Closures

Blocks can access variables in


surrounding
g scopes
p

all_words
words.each { | w | all_words = all_words + w + }

These

are almost, but not quite, first-class


closures as in Scheme (some differences in
scope rules)
l )

M
More
Bl
Block
kU
Uses

Besides iterating through containers,


blocks are used in many
y other contexts
3.times { puts hello }
n = 0
100.times { | k | n += k }
puts sum of 0 + + 99 is + n

Well

see more examples of blocks as well as


how to write code that uses them later

You might also like