SlideShare a Scribd company logo
Chapter 2
Choose The Right Control
There are 4 point:
• If, Unless, While & Until
• Use each, not for
• Case Logic
• False Value
1. If, Unless, While & Until
if & unless:
Usage:
if condition              unless condition
  do_something             do_something
end                       end
# do_something            # do_something
when condition is true.   when condition is false.
1. If, Unless, While & Until
if & unless without else statement:
Code: one line statement
“eat something” If hungry?

“stop eating” unless hungry?
“stop eating” if full?
**Not: “stop eating” if !hungry?
1. If, Unless, While & Until
if & unless with else statement:
Code:
If hungry?                  If full?
  “eat something”             “stop eating”
else                        else
  “stop eating”               “eat something”
end                         end
1. If, Unless, While & Until
if & unless with else statement:
**Not: never use unless with else statement
unless hungry?
  “stop eating”
else
  “eat something”
end
1. If, Unless, While & Until
Ternary:
Usage:
condition ? true : false
Code:
hungry? ? eat_something : stop_eating
full? ? stop_eating: eat_something
1. If, Unless, While & Until
while & until:
Usage:
while condition           until condition
 do_something              do_something
end                       end
# loop do_something       # loop do_something
when condition is true.   when condition is false.
1. If, Unless, While & Until
while & until:
Code: one line statement
eat_something while hungry?

stop_eating until hungry?
stop_eating while full?
**Not: stop_eating while !hungry?
2. Use each, Not for
Code:                           **Not:
array.each do |arr|             for arr in array
 puts arr                        puts arr
end                             end
                                when use this,
array.each { |arr| puts arr }   ruby will call each.
                                2 level calling.
3. Case Logic
Code:
case title
when 'War And Peace'
 puts 'Tolstoy'
when 'Romeo And Juliet'
 puts 'Shakespeare'
else
 puts "Don't know"
end
3. Case Logic
author = case title
         when 'War And Peace' then 'Tolstoy'
         when 'Romeo And Juliet' then 'Shakespeare'
         else "Don't know"
         end
3. Case Logic
case title
when /War And .*/
  puts 'Maybe Tolstoy?'
when /Romeo And .*/
  puts 'Maybe Shakespeare?'
else
  puts 'Absolutely no idea...'
end
4. False Value
Only nil and false are treated as false.
Code:
@first_name = ‘Chaung‘ unless @first_name
@first_name ||= ‘Chaung’

**Note: do not use this for boolean type
@boolean = false
@boolean ||= true
Thank You

More Related Content

PPT
Basic Grammar Run ons[2]
PPTX
Sentence transformation part 5
PPTX
Presentación1
PPTX
Nawaz anware predention english connectors
PPT
Run on Sentences
PPT
Run On Sentences
PPS
PPT
run-ons
Basic Grammar Run ons[2]
Sentence transformation part 5
Presentación1
Nawaz anware predention english connectors
Run on Sentences
Run On Sentences
run-ons

What's hot (19)

PPTX
English Punctuation | Period, Semicolon, Space
PPTX
Indefinite articles
PPTX
Comma rules
PPTX
Comma Rule 1
PPT
Macbeth Rough Draft 4
PPT
Dangling Modifiers
PPTX
Le présent
PPTX
Run-Ons
DOC
Tense Shift
PPTX
Comma Rule 2
PPTX
Avoiding Run-ons & Comma Splices
PPTX
Professional Sentence Patterns for Police Officers: Part II
PPT
Critique Rough Draft 3
PDF
Prefixes and suffixes second practices
PPT
Grammar
PPTX
Solving Verb Problems in Police Reports
PPTX
Clauses
PPTX
PT3 BAHASA INGGERIS 12 ( Section A)
PPTX
Three Uses for Truth Tables
English Punctuation | Period, Semicolon, Space
Indefinite articles
Comma rules
Comma Rule 1
Macbeth Rough Draft 4
Dangling Modifiers
Le présent
Run-Ons
Tense Shift
Comma Rule 2
Avoiding Run-ons & Comma Splices
Professional Sentence Patterns for Police Officers: Part II
Critique Rough Draft 3
Prefixes and suffixes second practices
Grammar
Solving Verb Problems in Police Reports
Clauses
PT3 BAHASA INGGERIS 12 ( Section A)
Three Uses for Truth Tables
Ad

Viewers also liked (20)

PDF
Software craftsmanship
PDF
Industrialisation Front-end - introduction
ODP
Open Close Principle
PPT
Poo java
PDF
Software Craftsmanship: Agile Is Not Enough
PDF
Tricks
PDF
Solid Principle
PPT
Programming Kiss
PDF
How to build SOLID code
PPTX
principle of oop’s in cpp
PDF
Don't Be STUPID, Grasp SOLID - ConFoo Edition
PDF
Ti1220 Lecture 7: Polymorphism
PPTX
Polymorphism presentation in java
PDF
The Open Closed Principle - Part 1 - The Original Version
KEY
SOLID Design Principles
PPTX
Software design principles
PPTX
Open Closed Principle kata
PDF
Partie 12: Polymorphisme — Programmation orientée objet en C++
PPT
SOLID principles
PDF
Application Programming Interface
Software craftsmanship
Industrialisation Front-end - introduction
Open Close Principle
Poo java
Software Craftsmanship: Agile Is Not Enough
Tricks
Solid Principle
Programming Kiss
How to build SOLID code
principle of oop’s in cpp
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Ti1220 Lecture 7: Polymorphism
Polymorphism presentation in java
The Open Closed Principle - Part 1 - The Original Version
SOLID Design Principles
Software design principles
Open Closed Principle kata
Partie 12: Polymorphisme — Programmation orientée objet en C++
SOLID principles
Application Programming Interface
Ad

Similar to Eloquen - Chapter 2 - use right control (20)

PPTX
5 Statements and Control Structures
PDF
09 ruby if else
KEY
Conditionals
PDF
Init() day2
PPTX
Ruby conditionals
PDF
Eloquent ruby
PPTX
Intro to ruby
PPT
L3 control
PDF
Seven Languages in Seven Days: Ruby
PPTX
RUBY PROGRAMMINGRUBY PROGRAMMING RUBY PROGRAMMING
PPT
Ruby Basics
 
PPT
Learning Ruby
PPTX
Ruby introduction part1
PPTX
Using loops
PDF
Conditional Statements
PPT
Cso gaddis java_chapter3
5 Statements and Control Structures
09 ruby if else
Conditionals
Init() day2
Ruby conditionals
Eloquent ruby
Intro to ruby
L3 control
Seven Languages in Seven Days: Ruby
RUBY PROGRAMMINGRUBY PROGRAMMING RUBY PROGRAMMING
Ruby Basics
 
Learning Ruby
Ruby introduction part1
Using loops
Conditional Statements
Cso gaddis java_chapter3

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Types and Its function , kingdom of life
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Lesson notes of climatology university.
PPTX
master seminar digital applications in india
PDF
Trump Administration's workforce development strategy
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Yogi Goddess Pres Conference Studio Updates
Final Presentation General Medicine 03-08-2024.pptx
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Complications of Minimal Access Surgery at WLH
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Updated Idioms and Phrasal Verbs in English subject
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Microbial disease of the cardiovascular and lymphatic systems
Cell Types and Its function , kingdom of life
STATICS OF THE RIGID BODIES Hibbelers.pdf
Paper A Mock Exam 9_ Attempt review.pdf.
Lesson notes of climatology university.
master seminar digital applications in india
Trump Administration's workforce development strategy
202450812 BayCHI UCSC-SV 20250812 v17.pptx
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE

Eloquen - Chapter 2 - use right control

  • 1. Chapter 2 Choose The Right Control There are 4 point: • If, Unless, While & Until • Use each, not for • Case Logic • False Value
  • 2. 1. If, Unless, While & Until if & unless: Usage: if condition unless condition do_something do_something end end # do_something # do_something when condition is true. when condition is false.
  • 3. 1. If, Unless, While & Until if & unless without else statement: Code: one line statement “eat something” If hungry? “stop eating” unless hungry? “stop eating” if full? **Not: “stop eating” if !hungry?
  • 4. 1. If, Unless, While & Until if & unless with else statement: Code: If hungry? If full? “eat something” “stop eating” else else “stop eating” “eat something” end end
  • 5. 1. If, Unless, While & Until if & unless with else statement: **Not: never use unless with else statement unless hungry? “stop eating” else “eat something” end
  • 6. 1. If, Unless, While & Until Ternary: Usage: condition ? true : false Code: hungry? ? eat_something : stop_eating full? ? stop_eating: eat_something
  • 7. 1. If, Unless, While & Until while & until: Usage: while condition until condition do_something do_something end end # loop do_something # loop do_something when condition is true. when condition is false.
  • 8. 1. If, Unless, While & Until while & until: Code: one line statement eat_something while hungry? stop_eating until hungry? stop_eating while full? **Not: stop_eating while !hungry?
  • 9. 2. Use each, Not for Code: **Not: array.each do |arr| for arr in array puts arr puts arr end end when use this, array.each { |arr| puts arr } ruby will call each. 2 level calling.
  • 10. 3. Case Logic Code: case title when 'War And Peace' puts 'Tolstoy' when 'Romeo And Juliet' puts 'Shakespeare' else puts "Don't know" end
  • 11. 3. Case Logic author = case title when 'War And Peace' then 'Tolstoy' when 'Romeo And Juliet' then 'Shakespeare' else "Don't know" end
  • 12. 3. Case Logic case title when /War And .*/ puts 'Maybe Tolstoy?' when /Romeo And .*/ puts 'Maybe Shakespeare?' else puts 'Absolutely no idea...' end
  • 13. 4. False Value Only nil and false are treated as false. Code: @first_name = ‘Chaung‘ unless @first_name @first_name ||= ‘Chaung’ **Note: do not use this for boolean type @boolean = false @boolean ||= true