SlideShare a Scribd company logo
RUBY – variable types,
brief intro
Variables?
●

●

●

●

Most of you know this already but lets just
review.
Variables reference memory locations used to
temporarily store data.
Variables exists for a certain time and then are
gone. This is the lifetime or extent of a variable.
The scope of a variable describes where in the
code the variable can be used and affects it's
extent.
General Things About Ruby Vars
●

●

●

●

Variables are not strongly typed - they can hold
any value at any time.
There are six types of variables - global, class,
instance, local, constants and there's also a
pseudo-variable type as well.
Case matters - constants begin with an
uppercase. Also variable 'apple' does not refer
to the same thing as 'aPPLE'.
There's more but I did say this was brief.
Sigils
●

●

A sigil is a special identifier attached to a
variable name.
A sigil will indicate a variable's data type or
scope.

●

In Ruby, variable sigils denote variable scope.

●

Ruby has three sigils '$', '@', and '@@'
Global Vars
●

●

●

Global variables are valid everywhere in the
script. They start with a '$' sigil in Ruby.
Their use is generally discouraged as in many
languages since they can lead to many
programming errors.
You'll commonly find global variables in the
Ruby environment.
Global Var Example
Class Vars
●
●

●

●

Class variables start with a '@@' sigil in Ruby.
A class variable is shared among all objects of
a class.
Only one variable value exists for all objects
instantiated from this class.
If one object instance changes the value of the
variable, that new value will essentially change
for all other object instances.
Class Var Example
Instance Vars
●
●

●

Instance variables start with a '@' sigil in Ruby.
Instance variables are variables that belong to
a particular object instance.
Objects of the same class have their own local
copies of the variable which are independent of
changes made in any other objects.
Instance Var Example
Local Vars
●

●

●

Local variables do not begin with a sigil but
begin with a lower case or underscore.
The scope of a local variable in limited to the
construct in which they are declared.
For example, a local variable declared in a
method or within a loop cannot be accessed
outside of that loop or method.
Local Var Example
Constant Vars
●

●

●

●

●

Constant vars do not begin with a sigil but they
must begin with a capital alpha char.
Ruby constants are values which, once
assigned a value, should not be changed.
I say 'should' because sigh..ruby lets you
change them..though you do get a warning.
Constants declared within a class or module
are available anywhere within the context of
that class or module.
Constants declared outside of a class or
module are assigned global scope.
Constant Var Example
Pseudo-variables
●

Special variables that have the appearance of
local variables but behave like constants. You
can not assign any value to these variables.
Pseudo-var Examples
●

self

- current object

●

true

- value representing true

●

false

- value representing false

●

nil

- value representing undefined

●

__FILE__ - name of the current source file

●

__LINE__ - current line # in the source file
THE END

More Related Content

PDF
Value Objects
PPT
QTP VB Script Trainings
PDF
JLS myths
PPTX
VB Script
PDF
Vbscript
PPT
VB Script Overview
PPTX
Javascript conditional statements
PPTX
C# Generics
Value Objects
QTP VB Script Trainings
JLS myths
VB Script
Vbscript
VB Script Overview
Javascript conditional statements
C# Generics

Similar to Intro to Ruby Variables (20)

PDF
03. Ruby Variables & Regex - Ruby Core Teaching
PPTX
Java session2
PPT
JavaScript Variables
PDF
data types.pdf
PDF
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
PPTX
Java_Roadmap.pptx
PDF
Subprogram
PPTX
Chapter-3 الشابتر الثالث هياكل بيانات جامعة خالد .pptx
PDF
Java Variable Types
PPTX
Java script session 3
DOCX
Ruby Interview Questions
PPTX
PPT
Core Java
PDF
Java Basics Presentation
PDF
7. VARIABLEs presentation in java programming. Pdf
PDF
8 introduction to_java_script
PDF
Introduction to Javascript and Typescript.pdf
PDF
23 concurrency-examples
PPTX
Unit 1-subroutines in perl
PPTX
Subroutines in perl
03. Ruby Variables & Regex - Ruby Core Teaching
Java session2
JavaScript Variables
data types.pdf
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Java_Roadmap.pptx
Subprogram
Chapter-3 الشابتر الثالث هياكل بيانات جامعة خالد .pptx
Java Variable Types
Java script session 3
Ruby Interview Questions
Core Java
Java Basics Presentation
7. VARIABLEs presentation in java programming. Pdf
8 introduction to_java_script
Introduction to Javascript and Typescript.pdf
23 concurrency-examples
Unit 1-subroutines in perl
Subroutines in perl
Ad

Recently uploaded (20)

PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced IT Governance
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Cloud computing and distributed systems.
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Chapter 2 Digital Image Fundamentals.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced IT Governance
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
20250228 LYD VKU AI Blended-Learning.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Review of recent advances in non-invasive hemoglobin estimation
Cloud computing and distributed systems.
Chapter 3 Spatial Domain Image Processing.pdf
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
GamePlan Trading System Review: Professional Trader's Honest Take
The Rise and Fall of 3GPP – Time for a Sabbatical?
CIFDAQ's Market Insight: SEC Turns Pro Crypto
madgavkar20181017ppt McKinsey Presentation.pdf
Understanding_Digital_Forensics_Presentation.pptx
Chapter 2 Digital Image Fundamentals.pdf
Big Data Technologies - Introduction.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Ad

Intro to Ruby Variables

  • 1. RUBY – variable types, brief intro
  • 2. Variables? ● ● ● ● Most of you know this already but lets just review. Variables reference memory locations used to temporarily store data. Variables exists for a certain time and then are gone. This is the lifetime or extent of a variable. The scope of a variable describes where in the code the variable can be used and affects it's extent.
  • 3. General Things About Ruby Vars ● ● ● ● Variables are not strongly typed - they can hold any value at any time. There are six types of variables - global, class, instance, local, constants and there's also a pseudo-variable type as well. Case matters - constants begin with an uppercase. Also variable 'apple' does not refer to the same thing as 'aPPLE'. There's more but I did say this was brief.
  • 4. Sigils ● ● A sigil is a special identifier attached to a variable name. A sigil will indicate a variable's data type or scope. ● In Ruby, variable sigils denote variable scope. ● Ruby has three sigils '$', '@', and '@@'
  • 5. Global Vars ● ● ● Global variables are valid everywhere in the script. They start with a '$' sigil in Ruby. Their use is generally discouraged as in many languages since they can lead to many programming errors. You'll commonly find global variables in the Ruby environment.
  • 7. Class Vars ● ● ● ● Class variables start with a '@@' sigil in Ruby. A class variable is shared among all objects of a class. Only one variable value exists for all objects instantiated from this class. If one object instance changes the value of the variable, that new value will essentially change for all other object instances.
  • 9. Instance Vars ● ● ● Instance variables start with a '@' sigil in Ruby. Instance variables are variables that belong to a particular object instance. Objects of the same class have their own local copies of the variable which are independent of changes made in any other objects.
  • 11. Local Vars ● ● ● Local variables do not begin with a sigil but begin with a lower case or underscore. The scope of a local variable in limited to the construct in which they are declared. For example, a local variable declared in a method or within a loop cannot be accessed outside of that loop or method.
  • 13. Constant Vars ● ● ● ● ● Constant vars do not begin with a sigil but they must begin with a capital alpha char. Ruby constants are values which, once assigned a value, should not be changed. I say 'should' because sigh..ruby lets you change them..though you do get a warning. Constants declared within a class or module are available anywhere within the context of that class or module. Constants declared outside of a class or module are assigned global scope.
  • 15. Pseudo-variables ● Special variables that have the appearance of local variables but behave like constants. You can not assign any value to these variables.
  • 16. Pseudo-var Examples ● self - current object ● true - value representing true ● false - value representing false ● nil - value representing undefined ● __FILE__ - name of the current source file ● __LINE__ - current line # in the source file