2 Slides Conditionals Iterators Blocks Hashes Arrays

27
Conditionals, Iterators& Blocks With Hashes & Arrays

Transcript of 2 Slides Conditionals Iterators Blocks Hashes Arrays

Conditionals, Iterators& Blocks

With Hashes & Arrays

Conditionals

Conditionals: if

if age > 17

puts “can vote”

end

if age > 17

puts “can vote”

else

puts “attends school”

end

Statement Modifiers:

y = 7 if x == 4

Other Syntax:if x == 4 then y = 7 end

Truth Truth: Everything is true except for: false nil

Therefore 0 is true “” is true

Checking for false:if !(name == “superman”) …

if not (name == “superman”) …

Unless

“unless” provides us with another way of checking if

a condition is false:

unless superpower == nil

status = “superhero”

end

Case

case superhero

when “superman”

city = “metropolis”

when “batman”

city = “gotham_city”

else

city = “central_city”

end

Case Refactoring

city = case superhero

when “superman”

“metropolis”

when “batman”

“gotham_city”

else

“central_city”

end

Iterators

Iterators: Conditional Looping

“while” allows us to loop through code while a set

condition is true

x = 1

while x< 10

puts x.to_s + “ iteration”

x += 1

end

Creating a new array

x = [1, 2, 3, 4]

=> [1, 2, 3, 4]

x = %w(1 2 3 4)

=> [“1”, “2”, “3”, “4”]

chef = Array.new(3, “bork”)

=> [“bork”, “bork”, bork”]

Accessing Array Values

a = [ "a", "b", "c", "d", "e" ]

a[0] #=> "a”

a[2] #=> "c”

a[6] #=> nil

a[1, 2] #=> ["b", "c”]

a[1..3] #=> ["b", "c", "d”]

a[1…3] #=> ["b", "c"]

Operations on Arrays

[ 1, 2, 3 ] * 3

=> [1, 2, 3, 1, 2, 3, 1, 2, 3]

[ 1, 2, 3 ].join(“,”)

"1,2,3”

[ 1, 2, 3 ] + [ 4, 5 ]

=> [1, 2, 3, 4, 5]

[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ]

=> [3, 3, 5]

[ 1, 2 ] << "c" << "d" << [ 3, 4 ]

=> [1, 2, "c", "d", [3, 4]]

Creating a Hash

h = { "a" => 100, "b" => 200 }

h[“a”]

h = { 1 => “a”, “b” => “hello” }

h[1]

Operations on Hashes: Merge

h1 = { "a" => 100, "b" => 200 }

=> {"a"=>100, "b"=>200}

h2 = { "b" => 254, "c" => 300 }

=>{"b"=>254, "c"=>300}

h3 = h1.merge(h2)

=> {"a"=>100, "b"=>254, "c"=>300}

h1

=> {"a"=>100, "b"=>200}

h1.merge!(h2)

=> {"a"=>100, "b"=>254, "c"=>300}

Operations on Hashes

h = { "a" => 100, "b" => 200 }

h.delete("a”)

h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }

letters = h.keys

h = { "a" => 100, "b" => 200, "c" => 300 }

numbers = h.values

Times

5.times{ puts “hello” }

99.times do |beer_num|

puts "#{beer_num} bottles of beer”

end

Eachsuperheroes = [“catwoman”, “batman”, “wonderwoman”]

superheroes.each { | s | puts “#{ s } save me!” }

wonderwoman save me!

batman save me!

catwoman save me!

dogs = ["fido", "fifi", "rex", "fluffy"]

dogs_i_want = []

dogs.each { |dog| dogs_i_want.push(dog) if dog != "fluffy"

}

>> dogs

=> ["fido", "fifi", "rex", "fluffy"]

>>dogs_i_want

=> ["fido", "fifi", "rex"]

Blocks

Blocks

def dos_veces

yield

yield

end

dos_veces { puts "Hola” }

Hola

Hola

This is a

Block!{Yield executes the

block

Yield with Parameters

def bands

yield(“abba”, “who”)

end

bands do |x,y| puts

x,y

end

abba

who

Yield sends its parameters as

arguments to the block

yield(“abba”, ”who”) sends

“abba” and “who” to |x, y|

x is set to “abba”

y is set to “who”

Block Syntax

{ |x| puts x}

is the same as:

do |x|

puts x

end

Performance Monitor: Blocks in Practice

Finding the bottleneck in a slow application

Stubs

Syntax:

A.stub!(:msg).and_return(:default)

A.stub!(:msg).with(1).and_return(:default)

Stubbing Time

How can we do this to help with Time.now?

t = Time.now

t + 10 #adds 10 seconds

Stubbing Time:

Time.stub!(:now).and_return{fake_time += 10}

How to Use Stubs in a Test:

describe “Time stub” do

it “should increment mock_time by 10 seconds” do

fake_time = 0

Time.stub!(:now).and_return { fake_time += 10 }

Time.now.should == 10

Time.now.should == 20

end

end

Your Turn

Homework

Chapters:

6.1-6.3

9.1-9.3

Koans:

-about_blocks

-about_iteration

-about_control_statements

-about_array_assignment

-about_arrays

-about_hashes