SlideShare a Scribd company logo
1 of 14
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

What's hot

English Punctuation | Period, Semicolon, Space
English Punctuation | Period, Semicolon, SpaceEnglish Punctuation | Period, Semicolon, Space
English Punctuation | Period, Semicolon, SpaceVirak Soeurn Un
 
Macbeth Rough Draft 4
Macbeth Rough Draft 4Macbeth Rough Draft 4
Macbeth Rough Draft 4eardales
 
Avoiding Run-ons & Comma Splices
Avoiding Run-ons & Comma Splices Avoiding Run-ons & Comma Splices
Avoiding Run-ons & Comma Splices Thalia Longoria
 
Professional Sentence Patterns for Police Officers: Part II
Professional Sentence Patterns for Police Officers: Part IIProfessional Sentence Patterns for Police Officers: Part II
Professional Sentence Patterns for Police Officers: Part IIJean Reynolds
 
Critique Rough Draft 3
Critique Rough Draft 3Critique Rough Draft 3
Critique Rough Draft 3eardales
 
Prefixes and suffixes second practices
Prefixes and suffixes second practicesPrefixes and suffixes second practices
Prefixes and suffixes second practicesAndres Ruiz
 
Solving Verb Problems in Police Reports
Solving Verb Problems in Police ReportsSolving Verb Problems in Police Reports
Solving Verb Problems in Police ReportsJean Reynolds
 
PT3 BAHASA INGGERIS 12 ( Section A)
PT3 BAHASA INGGERIS 12 ( Section A)PT3 BAHASA INGGERIS 12 ( Section A)
PT3 BAHASA INGGERIS 12 ( Section A)Miz Malinz
 
Three Uses for Truth Tables
Three Uses for Truth TablesThree Uses for Truth Tables
Three Uses for Truth Tablesdyeakel
 

What's hot (19)

English Punctuation | Period, Semicolon, Space
English Punctuation | Period, Semicolon, SpaceEnglish Punctuation | Period, Semicolon, Space
English Punctuation | Period, Semicolon, Space
 
Indefinite articles
Indefinite articlesIndefinite articles
Indefinite articles
 
Comma rules
Comma rulesComma rules
Comma rules
 
Comma Rule 1
Comma Rule 1Comma Rule 1
Comma Rule 1
 
Macbeth Rough Draft 4
Macbeth Rough Draft 4Macbeth Rough Draft 4
Macbeth Rough Draft 4
 
Dangling Modifiers
Dangling  ModifiersDangling  Modifiers
Dangling Modifiers
 
Le présent
Le présentLe présent
Le présent
 
Run-Ons
Run-OnsRun-Ons
Run-Ons
 
Tense Shift
Tense ShiftTense Shift
Tense Shift
 
Comma Rule 2
Comma Rule 2Comma Rule 2
Comma Rule 2
 
Avoiding Run-ons & Comma Splices
Avoiding Run-ons & Comma Splices Avoiding Run-ons & Comma Splices
Avoiding Run-ons & Comma Splices
 
Professional Sentence Patterns for Police Officers: Part II
Professional Sentence Patterns for Police Officers: Part IIProfessional Sentence Patterns for Police Officers: Part II
Professional Sentence Patterns for Police Officers: Part II
 
Critique Rough Draft 3
Critique Rough Draft 3Critique Rough Draft 3
Critique Rough Draft 3
 
Prefixes and suffixes second practices
Prefixes and suffixes second practicesPrefixes and suffixes second practices
Prefixes and suffixes second practices
 
Grammar
GrammarGrammar
Grammar
 
Solving Verb Problems in Police Reports
Solving Verb Problems in Police ReportsSolving Verb Problems in Police Reports
Solving Verb Problems in Police Reports
 
Clauses
ClausesClauses
Clauses
 
PT3 BAHASA INGGERIS 12 ( Section A)
PT3 BAHASA INGGERIS 12 ( Section A)PT3 BAHASA INGGERIS 12 ( Section A)
PT3 BAHASA INGGERIS 12 ( Section A)
 
Three Uses for Truth Tables
Three Uses for Truth TablesThree Uses for Truth Tables
Three Uses for Truth Tables
 

Viewers also liked

Industrialisation Front-end - introduction
Industrialisation Front-end - introductionIndustrialisation Front-end - introduction
Industrialisation Front-end - introductionhalleck45
 
Open Close Principle
Open Close PrincipleOpen Close Principle
Open Close PrincipleThaichor Seng
 
Software Craftsmanship: Agile Is Not Enough
Software Craftsmanship: Agile Is Not EnoughSoftware Craftsmanship: Agile Is Not Enough
Software Craftsmanship: Agile Is Not EnoughKen Auer
 
principle of oop’s in cpp
principle of oop’s in cppprinciple of oop’s in cpp
principle of oop’s in cppgourav kottawar
 
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo EditionDon't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo EditionAnthony Ferrara
 
Ti1220 Lecture 7: Polymorphism
Ti1220 Lecture 7: PolymorphismTi1220 Lecture 7: Polymorphism
Ti1220 Lecture 7: PolymorphismEelco Visser
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in javaAhsan Raja
 
The Open Closed Principle - Part 1 - The Original Version
The Open Closed Principle - Part 1 - The Original VersionThe Open Closed Principle - Part 1 - The Original Version
The Open Closed Principle - Part 1 - The Original VersionPhilip Schwarz
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design PrinciplesSamuel Breed
 
Software design principles
Software design principlesSoftware design principles
Software design principlesMd.Mojibul Hoque
 
Open Closed Principle kata
Open Closed Principle kataOpen Closed Principle kata
Open Closed Principle kataPaul Blundell
 
Partie 12: Polymorphisme — Programmation orientée objet en C++
Partie 12: Polymorphisme — Programmation orientée objet en C++Partie 12: Polymorphisme — Programmation orientée objet en C++
Partie 12: Polymorphisme — Programmation orientée objet en C++Fabio Hernandez
 
Application Programming Interface
Application Programming InterfaceApplication Programming Interface
Application Programming InterfaceManifest Infotech
 

Viewers also liked (20)

Software craftsmanship
Software craftsmanshipSoftware craftsmanship
Software craftsmanship
 
Industrialisation Front-end - introduction
Industrialisation Front-end - introductionIndustrialisation Front-end - introduction
Industrialisation Front-end - introduction
 
Open Close Principle
Open Close PrincipleOpen Close Principle
Open Close Principle
 
Poo java
Poo javaPoo java
Poo java
 
Software Craftsmanship: Agile Is Not Enough
Software Craftsmanship: Agile Is Not EnoughSoftware Craftsmanship: Agile Is Not Enough
Software Craftsmanship: Agile Is Not Enough
 
Tricks
TricksTricks
Tricks
 
Solid Principle
Solid PrincipleSolid Principle
Solid Principle
 
Programming Kiss
Programming KissProgramming Kiss
Programming Kiss
 
How to build SOLID code
How to build SOLID codeHow to build SOLID code
How to build SOLID code
 
principle of oop’s in cpp
principle of oop’s in cppprinciple of oop’s in cpp
principle of oop’s in cpp
 
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo EditionDon't Be STUPID, Grasp SOLID - ConFoo Edition
Don't Be STUPID, Grasp SOLID - ConFoo Edition
 
Ti1220 Lecture 7: Polymorphism
Ti1220 Lecture 7: PolymorphismTi1220 Lecture 7: Polymorphism
Ti1220 Lecture 7: Polymorphism
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
 
The Open Closed Principle - Part 1 - The Original Version
The Open Closed Principle - Part 1 - The Original VersionThe Open Closed Principle - Part 1 - The Original Version
The Open Closed Principle - Part 1 - The Original Version
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
Software design principles
Software design principlesSoftware design principles
Software design principles
 
Open Closed Principle kata
Open Closed Principle kataOpen Closed Principle kata
Open Closed Principle kata
 
Partie 12: Polymorphisme — Programmation orientée objet en C++
Partie 12: Polymorphisme — Programmation orientée objet en C++Partie 12: Polymorphisme — Programmation orientée objet en C++
Partie 12: Polymorphisme — Programmation orientée objet en C++
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
 
Application Programming Interface
Application Programming InterfaceApplication Programming Interface
Application Programming Interface
 

Recently uploaded

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 

Recently uploaded (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 

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