SlideShare ist ein Scribd-Unternehmen logo
1 von 11
CONDITIONALS




    Sarah Allen
TRUTH

• Truth: Everything is true except for:
  – false
  – nil

• Therefore
  – 0 is true
  – “” is true
CONDITIONALS: IF
if age > 17
  puts “can vote”
                    Statement Modifiers:
end                 do_something if x == 4

                    Other Syntax:
                    if x == 4 then do_something end
if age > 17
  puts “can vote”
else
  puts “attends school”
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

Weitere ähnliche Inhalte

Mehr von Blazing Cloud

RSpec Quick Reference
RSpec Quick ReferenceRSpec Quick Reference
RSpec Quick Reference
Blazing Cloud
 
Interactive Graphics
Interactive GraphicsInteractive Graphics
Interactive Graphics
Blazing Cloud
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Blazing Cloud
 

Mehr von Blazing Cloud (20)

Rails Class Intro - 1
Rails Class Intro - 1 Rails Class Intro - 1
Rails Class Intro - 1
 
RSpec Quick Reference
RSpec Quick ReferenceRSpec Quick Reference
RSpec Quick Reference
 
Extending rails
Extending railsExtending rails
Extending rails
 
2day Ruby Class Intro
2day Ruby Class Intro2day Ruby Class Intro
2day Ruby Class Intro
 
Mobile Lean UX
Mobile Lean UXMobile Lean UX
Mobile Lean UX
 
Interactive Graphics
Interactive GraphicsInteractive Graphics
Interactive Graphics
 
Interactive Graphics w/ Javascript, HTML5 and CSS3
Interactive Graphics w/ Javascript, HTML5 and CSS3Interactive Graphics w/ Javascript, HTML5 and CSS3
Interactive Graphics w/ Javascript, HTML5 and CSS3
 
Form helpers
Form helpersForm helpers
Form helpers
 
Intro to Ruby (and RSpec)
Intro to Ruby (and RSpec)Intro to Ruby (and RSpec)
Intro to Ruby (and RSpec)
 
What you don't know (yet)
What you don't know (yet)What you don't know (yet)
What you don't know (yet)
 
Introduction to Rails
Introduction to RailsIntroduction to Rails
Introduction to Rails
 
ActiveRecord
ActiveRecordActiveRecord
ActiveRecord
 
Ruby on Rails Class intro
Ruby on Rails Class introRuby on Rails Class intro
Ruby on Rails Class intro
 
Ruby on rails toolbox
Ruby on rails toolboxRuby on rails toolbox
Ruby on rails toolbox
 
Routes Controllers
Routes ControllersRoutes Controllers
Routes Controllers
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Active Record
Active RecordActive Record
Active Record
 
Enumerables
EnumerablesEnumerables
Enumerables
 
Reg EX
Reg EXReg EX
Reg EX
 
Ruby Blocks
Ruby BlocksRuby Blocks
Ruby Blocks
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Conditionals

  • 1. CONDITIONALS Sarah Allen
  • 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” Statement Modifiers: end do_something if x == 4 Other Syntax: if x == 4 then do_something end if age > 17 puts “can vote” else puts “attends school” 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

Hinweis der Redaktion

  1. \n
  2. Unlike some languages with the 0 and empty string\n! Binds more tightly than than the “not” keyword so you do need parentheses for example 1, but don’t need parentheses for example 2\n
  3. Conditionals are key to being able to make decisions in a program\n\nleft looks like every other language\n\nparentheses are optional in ruby \nmake sure to do ==, = is an assignment, == is a conditional test\n\nExplain puts\n\nright is a little different…people in ruby don’t like to type…english readable\nA statement modifier lets you move control structures at the end of an expression.\n
  4. Conditionals are key to being able to make decisions in a program\n\nleft looks like every other language\n\nparentheses are optional in ruby \nmake sure to do ==, = is an assignment, == is a conditional test\n\nExplain puts\n\nright is a little different…people in ruby don’t like to type…english readable\nA statement modifier lets you move control structures at the end of an expression.\n
  5. \n
  6. Unless can be awkward, especially with else. Usually you’ll want to use if for conditionals with else clauses.\n Occasionally unless is more readable:\n unless something is nil\n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n