SlideShare a Scribd company logo
1 of 11
Conditionals
Truth Truth: Everything is true except for: false  nil Therefore 0 is true “” is true
Conditionals: if if age > 17 	puts “can vote” end if age > 17 	puts “can vote” else 	puts “attends school” end Statement Modifiers: do_something if x == 4 Other Syntax: if x == 4 then do_something end
Conditionals: if if age >= 21  	  puts “can drink” elsif age > 17     puts “can vote” else  	  puts “too young” end
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 human 		status = "superhero" 	end 	status = "superhero" unless human
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
Case case num when 1 puts "one" when 2..5 puts "that is small" else	 puts "pretty big" end
Case case name when "Sarah" puts "awesome" when /Mr.*/ puts "formal guy" else	 puts "whatever" end
Case case args 		when String 			puts "Got a String"   	when Array 			puts "Got an Array”  	when Hash 			puts "Got a Hash" end

More Related Content

More from Sarah Allen

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesSarah Allen
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)Sarah Allen
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical SkillSarah Allen
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government ServicesSarah Allen
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of ComputersSarah Allen
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software FunSarah Allen
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of TransparencySarah Allen
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for FunSarah Allen
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceSarah Allen
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead PeopleSarah Allen
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby Sarah Allen
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Sarah Allen
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?Sarah Allen
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersSarah Allen
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentSarah Allen
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeSarah Allen
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future ThoughtsSarah Allen
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web VideoSarah Allen
 
Elementary Computer History
Elementary Computer HistoryElementary Computer History
Elementary Computer HistorySarah Allen
 
Sarah Allen Computer Science Entrepreneur
Sarah Allen Computer Science EntrepreneurSarah Allen Computer Science Entrepreneur
Sarah Allen Computer Science EntrepreneurSarah Allen
 

More from Sarah Allen (20)

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequences
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical Skill
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government Services
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of Computers
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software Fun
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of Transparency
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for Fun
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World Conference
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead People
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from Toastmasters
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product Development
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription Landscape
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future Thoughts
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web Video
 
Elementary Computer History
Elementary Computer HistoryElementary Computer History
Elementary Computer History
 
Sarah Allen Computer Science Entrepreneur
Sarah Allen Computer Science EntrepreneurSarah Allen Computer Science Entrepreneur
Sarah Allen Computer Science Entrepreneur
 

Ruby conditionals

  • 2. Truth Truth: Everything is true except for: false nil Therefore 0 is true “” is true
  • 3. Conditionals: if if age > 17 puts “can vote” end if age > 17 puts “can vote” else puts “attends school” end Statement Modifiers: do_something if x == 4 Other Syntax: if x == 4 then do_something end
  • 4. Conditionals: if if age >= 21 puts “can drink” elsif age > 17 puts “can vote” else puts “too young” end
  • 5. Checking for false if !(name == “superman”) … if not (name == “superman”) …
  • 6. Unless “unless” provides us with another way of checking if a condition is false: unless human status = "superhero" end status = "superhero" unless human
  • 7. Case case superhero when "superman" city = "metropolis" when "batman" city = "gotham_city" else city = "central_city" end
  • 8. Case Refactoring city = case superhero when "superman" "metropolis" when "batman" "gotham_city" else "central_city" end
  • 9. Case case num when 1 puts "one" when 2..5 puts "that is small" else puts "pretty big" end
  • 10. Case case name when "Sarah" puts "awesome" when /Mr.*/ puts "formal guy" else puts "whatever" end
  • 11. Case case args when String puts "Got a String" when Array puts "Got an Array” when Hash puts "Got a Hash" end

Editor's Notes

  1. 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
  2. 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.
  3. 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.
  4. 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