SlideShare a Scribd company logo
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(“,”) ,[object Object],[ 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
Each superheroes = [“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 Yield executes the block This is a 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

More Related Content

Similar to 2 Slides Conditionals Iterators Blocks Hashes Arrays

Learning Ruby
Learning RubyLearning Ruby
Learning Ruby
Overdue Books LLC
 
Ruby Language: Array, Hash and Iterators
Ruby Language: Array, Hash and IteratorsRuby Language: Array, Hash and Iterators
Ruby Language: Array, Hash and Iterators
Sarah Allen
 
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Suyeol Jeon
 
Itty bittypresentation lrug
Itty bittypresentation lrugItty bittypresentation lrug
Itty bittypresentation lrug
Skills Matter
 
Itty bittypresentation lrug
Itty bittypresentation lrugItty bittypresentation lrug
Itty bittypresentation lrug
Tom Crinson
 
Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, MarakanaWhy Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, Marakana
Marko Gargenta
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby
Gautam Rege
 
Speeding up Red Team engagements with carnivorall
Speeding up Red Team engagements with carnivorallSpeeding up Red Team engagements with carnivorall
Speeding up Red Team engagements with carnivorall
Nullbyte Security Conference
 
Bash Geekcamp
Bash GeekcampBash Geekcamp
Bash Geekcamp
guest7f4365d
 
Modern Perl
Modern PerlModern Perl
Modern Perl
Marcos Rebelo
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
勇浩 赖
 
Php 2
Php 2Php 2
Php 2
vivlinux
 
#include iostream #include string #include iomanip #incl.pdf
#include iostream #include string #include iomanip #incl.pdf#include iostream #include string #include iomanip #incl.pdf
#include iostream #include string #include iomanip #incl.pdf
sudhinjv
 
Nitrificacion importancia medio ambiental bacterias nitrificantes
Nitrificacion importancia medio ambiental bacterias nitrificantesNitrificacion importancia medio ambiental bacterias nitrificantes
Nitrificacion importancia medio ambiental bacterias nitrificantes
LUIS FERNANDO QUIROGA PUERTA
 
Bash Scripting Gabrovo
Bash Scripting GabrovoBash Scripting Gabrovo
Bash Scripting Gabrovo
Marian Marinov
 
Tic tac toe
Tic tac toeTic tac toe
Tic tac toe
jo pakson
 
Ruby 程式語言簡介
Ruby 程式語言簡介Ruby 程式語言簡介
Ruby 程式語言簡介
Wen-Tien Chang
 
The bones of a nice Python script
The bones of a nice Python scriptThe bones of a nice Python script
The bones of a nice Python script
saniac
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
Sopan Shewale
 
Exploiting Php With Php
Exploiting Php With PhpExploiting Php With Php
Exploiting Php With Php
Jeremy Coates
 

Similar to 2 Slides Conditionals Iterators Blocks Hashes Arrays (20)

Learning Ruby
Learning RubyLearning Ruby
Learning Ruby
 
Ruby Language: Array, Hash and Iterators
Ruby Language: Array, Hash and IteratorsRuby Language: Array, Hash and Iterators
Ruby Language: Array, Hash and Iterators
 
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
 
Itty bittypresentation lrug
Itty bittypresentation lrugItty bittypresentation lrug
Itty bittypresentation lrug
 
Itty bittypresentation lrug
Itty bittypresentation lrugItty bittypresentation lrug
Itty bittypresentation lrug
 
Why Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, MarakanaWhy Python by Marilyn Davis, Marakana
Why Python by Marilyn Davis, Marakana
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby
 
Speeding up Red Team engagements with carnivorall
Speeding up Red Team engagements with carnivorallSpeeding up Red Team engagements with carnivorall
Speeding up Red Team engagements with carnivorall
 
Bash Geekcamp
Bash GeekcampBash Geekcamp
Bash Geekcamp
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
 
Php 2
Php 2Php 2
Php 2
 
#include iostream #include string #include iomanip #incl.pdf
#include iostream #include string #include iomanip #incl.pdf#include iostream #include string #include iomanip #incl.pdf
#include iostream #include string #include iomanip #incl.pdf
 
Nitrificacion importancia medio ambiental bacterias nitrificantes
Nitrificacion importancia medio ambiental bacterias nitrificantesNitrificacion importancia medio ambiental bacterias nitrificantes
Nitrificacion importancia medio ambiental bacterias nitrificantes
 
Bash Scripting Gabrovo
Bash Scripting GabrovoBash Scripting Gabrovo
Bash Scripting Gabrovo
 
Tic tac toe
Tic tac toeTic tac toe
Tic tac toe
 
Ruby 程式語言簡介
Ruby 程式語言簡介Ruby 程式語言簡介
Ruby 程式語言簡介
 
The bones of a nice Python script
The bones of a nice Python scriptThe bones of a nice Python script
The bones of a nice Python script
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Exploiting Php With Php
Exploiting Php With PhpExploiting Php With Php
Exploiting Php With Php
 

Recently uploaded

S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 

Recently uploaded (20)

S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 

2 Slides Conditionals Iterators Blocks Hashes Arrays

  • 1. Conditionals, Iterators & Blocks With Hashes & Arrays
  • 3. 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
  • 4. 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”) …
  • 5. Unless “unless” provides us with another way of checking if a condition is false: unless superpower == nil status = “superhero” end
  • 6. Case case superhero when “superman” city = “metropolis” when “batman” city = “gotham_city” else city = “central_city” end
  • 7. Case Refactoring city = case superhero when “superman” “metropolis” when “batman” “gotham_city” else “central_city” end
  • 9. 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
  • 10. 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”]
  • 11. 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"]
  • 12.
  • 13. Creating a Hash h = { "a" => 100, "b" => 200 } h[“a”] h = { 1 => “a”, “b” => “hello” } h[1]
  • 14. 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}
  • 15. 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
  • 16. Times 5.times{ puts “hello” } 99.times do |beer_num| puts "#{beer_num} bottles of beer” end
  • 17. Each superheroes = [“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"]
  • 19. Blocks def dos_veces yield yield end dos_veces { puts "Hola” } Hola Hola Yield executes the block This is a Block! {
  • 20. 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”
  • 21. Block Syntax { |x| puts x} is the same as: do |x| puts x end
  • 22. Performance Monitor: Blocks in Practice Finding the bottleneck in a slow application
  • 23. Stubs Syntax: A.stub!(:msg).and_return(:default) A.stub!(:msg).with(1).and_return(:default)
  • 24. 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}
  • 25. 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
  • 27. 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

Editor's Notes

  1. Conditionals are key to being able to make decisions in a programleft looks like every other languageparentheses are optional in ruby make sure to do ==, = is an assignment, == is a conditional testExplain putsright is a little different…people in ruby don’t like to type…english readableA statement modifier lets you move control structures at the end of an expression.
  2. Unlike some languages with the 0 and empty string! Binds more tightly than than the “not” keyword so you do need parentheses for example 1, but don’t need parentheses for example 2
  3. Unless can be awkward, especially with else. Usually you’ll want to use if for conditionals with else clauses.Occasionally unless is more readable:unless something is nil
  4. Alot of the time you will be using an array when you iterate over somethingAn array is just a list of items.Every spot in the list acts like a variable and you can make each spot point to a different objectW means wordsArray is a class, needs to start with capital letter
  5. IRBif you go off the array it will be nil
  6. join is cool because it makes a string for youshovel operatormultidimensional array
  7. Does anyone know what a hash is? associative array collection of key-value pairskeys can be numbers or strings Difference from an Array
  8. merge takes the value from the second hashmerge! changes h1
  9. you would think that delete should need a bang to change the hash, but delete doesn’t exist with a bangdelete returns the value
  10. 5 is an object that is an instance of the integer classtimes is a method of the 5 objecttimes is a method on an object that is an instance of integer
  11. it does the block of code three timesit is very rare that you will see a while loop in ruby...you can do the loops we did earlier, but rubyists will mock you.
  12. What is a block? It is the ability to take a block of code, wrap it up in an object and pass it to a method. Then you can run the block of code within the method any time you want…sometimes twice! The result is kind of like sending a method to a method, except that a block isn’t bound to an object like a method is – it is an object. So what? Why use blocks?elegant syntax for iteratorsBecause there are some things that only blocks can do, like being passed to a method and being returned by a method.
  13. two ways to declare a blockuse curly brackets for single lines of codeuse do end for multi lines of code