SlideShare a Scribd company logo
1 of 13
Variable Scope
         Or
    Prisons 101
Variable types

• Local
• Instance
• Class
• Global
Local Variable
• Syntax: all lowercase and underscores
• Example: prisoner_1

• Key Idea: Stuck inside a method
Prisoners can’t move to different
          rooms on their own.
San Quentin = Prison.new

                           def wardens_office
 def cell_1
                            warden= prisoner_1
  prisoner_1 = martha
                           end
 end




                                           def cell_2
                                             prisoner_2 = prisoner_1
                                           end
Class Prison

  def cell_1
     prisoner1 = martha
     prisoner2 = prisoner1
  end
  def warden_office
     ‘doesn’t know anything about ‘prisoner1’
  end

end
Instance Variable
• Syntax: starts with ‘@’
• Example: @warden

• Key Idea: Can be used across all methods in a
  class instance
Warden can move to any room in HIS
  prison. When he is changed it affects
       all methods in THIS prison
San Quentin = Prison.new
  @warden =                      def wardens_office_1
end                               warden= prisoner_1
       def cell_1                 Other_variable = @warden
        prisoner_1 = martha      end
        new_variable = @warden
       end

                                            def cell_2
                                              prisoner_2 = prisoner_1
                                              new_variable = @warden
                                            end
Class Prison

   attr_accessor :warden
   def initialize
       @warden
   end

   def cell_1
       new_variable = @warden
   end
   def warden_office
       other_variable = @warden
   end

end
Class Variable
• Syntax: starts with ‘@@’
• Example: @@Commisioner_of_prisons



• Key Idea:
  – Can be changed by any instance,
  – It is changed for ALL instances of the class
Warden can move to any room in HIS
   prison. When he is changed it affects
        all methods in THIS prison
San Quentin = Prison.new             Rickers_Island = Prison.new
 @@commisioner = pipe_man             @@commisioner = pipe_man
                            linked
end                                  end


      cell_1

                                       cell_2
Global Variable
• Syntax: starts with ‘$’
• Example: $president

• Key idea: Can go anywhere in your program.
  When changed it affects every place it is used.
  – Any class or method.
When changed improperly , a lot of
 unexpected bad crap happens.
• 911
• Iraq war
• Financial Crisis



  –So just don’t use it.

More Related Content

Viewers also liked

Managing Digital Footprints
Managing Digital FootprintsManaging Digital Footprints
Managing Digital FootprintsBernard Goldbach
 
Matt's resume. word doc (1)
Matt's resume. word doc (1)Matt's resume. word doc (1)
Matt's resume. word doc (1)Matthew McKinney
 
In Rem Injunctions: Case of Website Blocking
In Rem Injunctions: Case of Website BlockingIn Rem Injunctions: Case of Website Blocking
In Rem Injunctions: Case of Website BlockingMartin Husovec
 
Technik.hotelarstwa 341[04] z4.01_u
Technik.hotelarstwa 341[04] z4.01_uTechnik.hotelarstwa 341[04] z4.01_u
Technik.hotelarstwa 341[04] z4.01_uPusiu99
 
[NOTES] When Your Community Does the Blogging | MuseumNext Indy
[NOTES] When Your Community Does the Blogging | MuseumNext Indy[NOTES] When Your Community Does the Blogging | MuseumNext Indy
[NOTES] When Your Community Does the Blogging | MuseumNext IndyLori Byrd-McDevitt
 
National Diploma of N Mabunda.PDF
National Diploma of N Mabunda.PDFNational Diploma of N Mabunda.PDF
National Diploma of N Mabunda.PDFnicky mabunda
 
How to See People Who Block You on Twitter
How to See People Who Block You on TwitterHow to See People Who Block You on Twitter
How to See People Who Block You on TwitterBernard Goldbach
 
The essentials
The essentialsThe essentials
The essentialswaspronto
 

Viewers also liked (12)

Managing Digital Footprints
Managing Digital FootprintsManaging Digital Footprints
Managing Digital Footprints
 
Matt's resume. word doc (1)
Matt's resume. word doc (1)Matt's resume. word doc (1)
Matt's resume. word doc (1)
 
Ramayana Tour Legend Comes Alive in Sri Lanka
Ramayana Tour Legend Comes Alive in Sri LankaRamayana Tour Legend Comes Alive in Sri Lanka
Ramayana Tour Legend Comes Alive in Sri Lanka
 
In Rem Injunctions: Case of Website Blocking
In Rem Injunctions: Case of Website BlockingIn Rem Injunctions: Case of Website Blocking
In Rem Injunctions: Case of Website Blocking
 
Technik.hotelarstwa 341[04] z4.01_u
Technik.hotelarstwa 341[04] z4.01_uTechnik.hotelarstwa 341[04] z4.01_u
Technik.hotelarstwa 341[04] z4.01_u
 
[NOTES] When Your Community Does the Blogging | MuseumNext Indy
[NOTES] When Your Community Does the Blogging | MuseumNext Indy[NOTES] When Your Community Does the Blogging | MuseumNext Indy
[NOTES] When Your Community Does the Blogging | MuseumNext Indy
 
National Diploma of N Mabunda.PDF
National Diploma of N Mabunda.PDFNational Diploma of N Mabunda.PDF
National Diploma of N Mabunda.PDF
 
How to See People Who Block You on Twitter
How to See People Who Block You on TwitterHow to See People Who Block You on Twitter
How to See People Who Block You on Twitter
 
Resume
ResumeResume
Resume
 
Resume
ResumeResume
Resume
 
Blank Screen Creativity
Blank Screen CreativityBlank Screen Creativity
Blank Screen Creativity
 
The essentials
The essentialsThe essentials
The essentials
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 

Variable scope

  • 1. Variable Scope Or Prisons 101
  • 2. Variable types • Local • Instance • Class • Global
  • 3. Local Variable • Syntax: all lowercase and underscores • Example: prisoner_1 • Key Idea: Stuck inside a method
  • 4. Prisoners can’t move to different rooms on their own. San Quentin = Prison.new def wardens_office def cell_1 warden= prisoner_1 prisoner_1 = martha end end def cell_2 prisoner_2 = prisoner_1 end
  • 5. Class Prison def cell_1 prisoner1 = martha prisoner2 = prisoner1 end def warden_office ‘doesn’t know anything about ‘prisoner1’ end end
  • 6. Instance Variable • Syntax: starts with ‘@’ • Example: @warden • Key Idea: Can be used across all methods in a class instance
  • 7. Warden can move to any room in HIS prison. When he is changed it affects all methods in THIS prison San Quentin = Prison.new @warden = def wardens_office_1 end warden= prisoner_1 def cell_1 Other_variable = @warden prisoner_1 = martha end new_variable = @warden end def cell_2 prisoner_2 = prisoner_1 new_variable = @warden end
  • 8. Class Prison attr_accessor :warden def initialize @warden end def cell_1 new_variable = @warden end def warden_office other_variable = @warden end end
  • 9. Class Variable • Syntax: starts with ‘@@’ • Example: @@Commisioner_of_prisons • Key Idea: – Can be changed by any instance, – It is changed for ALL instances of the class
  • 10. Warden can move to any room in HIS prison. When he is changed it affects all methods in THIS prison San Quentin = Prison.new Rickers_Island = Prison.new @@commisioner = pipe_man @@commisioner = pipe_man linked end end cell_1 cell_2
  • 11. Global Variable • Syntax: starts with ‘$’ • Example: $president • Key idea: Can go anywhere in your program. When changed it affects every place it is used. – Any class or method.
  • 12. When changed improperly , a lot of unexpected bad crap happens.
  • 13. • 911 • Iraq war • Financial Crisis –So just don’t use it.