Ruby conditionals

1,205 views

Published on

Slides for Ruby Class: Conditionals

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,205
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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
  • 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.
  • 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.
  • 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
  • Ruby conditionals

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

    ×