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

Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
panagenda
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
UK Journal
 

Kürzlich hochgeladen (20)

Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Your enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4jYour enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4j
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 

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