SlideShare a Scribd company logo
RUBY
VARIABLES &
REGULAR
EXPRESSION
Hoang Quan(BlazingRockStorm)
Contents
VARIABLE NAMING RULES
LOCAL VARIABLES
INSTANCE, CLASS VARIABLES
SIGIL
GLOBAL VARIABLES
ENVIRONMENT & COMMAND-LINE
VARIABLES
PSEUDO
REGULAR EXPRESSION
Variable naming rules
Variable
naming rules
A variable is a place to store data.
Each variable is given a unique name.
There are some naming conventions
which apply to variable names.
Ruby belongs to the family of dynamic
languages. Unlike strongly typed
languages like Java, C or Pascal,
dynamic languages do not declare a
variable to be of certain data type.
Instead of that, the interpreter
determines the data type at the moment
of the assignment.
Variables in Ruby can contain different
values and different types of values over
time.
Variable naming rules
Ruby is a case sensitive language.
It means that age and Age are two different variable names. Most
languages are case sensitive.
While we can create different names by changing the case of the
characters, this practice is not recommended.
Variable naming rules
Variable names in Ruby can be created from alphanumeric
characters and the underscore _ character.
A variable cannot begin with a number. This makes it easier for
the interpreter to distinguish a literal number from a variable.
Variable names cannot begin with a capital letter. If an identifier
begins with a capital letter, it is considered to be a constant in
Ruby.
Variable naming rules
The script shows three descriptive variable names. The
place_of_birth is more descriptive to a programmer than e.g. pob. It
is generally considered OK to choose simple variable names in loops.
Sigils
Sigils
Variable identifiers can start with special characters also called
sigils.
A sigil is a symbol attached to an identifier.
Variable sigils in Ruby denote variable scope.
The Ruby variable sigils are $ and @.
Local variable
Local variable
Local variables are variables that are valid within a local area of a
Ruby source code.
This area is also referred to as local scope. Local variables exist
within the definition of a Ruby module, method, class.
If we access the variable outside the scope of the method, Ruby will
issue a NameError because Ruby does not find the variable.
Local variable
Global variables
Global variables
Global variables are valid everywhere in the code.
They start with a $ sigil in Ruby.
The use of global variables is discouraged.
Global variables easily lead to many programming errors.
Global variables should be used only when there is a reason to
do so.
Instead of global variables, programmers are advised to use local
variables whenever possible.
Global variables
Instance, class variables
Instance & class variables
In this section we will briefly cover instance and class variables. They
will be described in Object-oriented programming chapter in more
detail.
Instance variables are variables that belong to a particular
object instance. Each object has its own object variables.
Instance variables start with a @ sigil.
Class variables belong to a specific class. All objects created
from a particular class share class variables. Class variables start
with @@ characters.
Instance & class variables
Environment & command-
line variables
Environment & command-
line variables
The ENV constant gives access to environment variables. It is a
Ruby hash. Each environment variable is a key to the ENV hash.
The ARGV constant holds command-line argument values. They are
passed by the programmer when the script is launched. The ARGV
is an array that stores the arguments as strings. The $* is an alias to
the ARGV.
Both ENV and ARGV are global constants.
Environment & command-
line variables
Environment & command-
line variables
Pseudo variables
Pseudo variables
Ruby has a few variables which are called pseudo variables. They are
different from regular variables. We cannot assign values to pseudo
variables
The self is the receiver of the current method.
The nil is the sole instance of the NilClass. It represents the
absense of a value.
The true is the sole instance of the TrueClass. It represents
boolean true.
The false is a sole instance of FalseClass. It represents boolean
false.
Regular
Expression(RegEx)
RegEx
A regular expression is a special sequence of characters that helps
you match or find other strings or sets of strings using a specialized
syntax held in a pattern.
Thank you!
ありがとうございます!

More Related Content

Similar to 03. Ruby Variables & Regex - Ruby Core Teaching (20)

Ruby Interview Questions
Ruby Interview Questions
Sumanth krishna
 
Ruby
Ruby
Vladimir Bystrov
 
Java_Roadmap.pptx
Java_Roadmap.pptx
ssuser814cf2
 
Ruby basics
Ruby basics
Tushar Pal
 
Perl_Part1
Perl_Part1
Frank Booth
 
Grooming with Groovy
Grooming with Groovy
Dhaval Dalal
 
00 ruby tutorial
00 ruby tutorial
Walker Maidana
 
Variables
Variables
Samuel Santos
 
Introduction to Ruby
Introduction to Ruby
Mark Menard
 
java
java
Kunal Sunesara
 
Page List & Sample Material (Repaired)
Page List & Sample Material (Repaired)
Muhammad Haseeb Shahid
 
7-Java Language Basics Part1
7-Java Language Basics Part1
Amr Elghadban (AmrAngry)
 
Unit 1-subroutines in perl
Unit 1-subroutines in perl
sana mateen
 
Subroutines in perl
Subroutines in perl
sana mateen
 
Intro to Scala
Intro to Scala
manaswinimysore
 
Iq rails
Iq rails
Chatrapathi Yerra
 
06 ruby variables
06 ruby variables
Walker Maidana
 
Java Basics Presentation
Java Basics Presentation
Omid Sohrabi
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
sagaroceanic11
 

More from quanhoangd129 (7)

01. Ruby Introduction - Ruby Core Teaching
01. Ruby Introduction - Ruby Core Teaching
quanhoangd129
 
09. Ruby Object Oriented Programming - Ruby Core Teaching
09. Ruby Object Oriented Programming - Ruby Core Teaching
quanhoangd129
 
07. Ruby String Slides - Ruby Core Teaching
07. Ruby String Slides - Ruby Core Teaching
quanhoangd129
 
06. Ruby Array & Hash - Ruby Core Teaching
06. Ruby Array & Hash - Ruby Core Teaching
quanhoangd129
 
05. Ruby Control Structures - Ruby Core Teaching
05. Ruby Control Structures - Ruby Core Teaching
quanhoangd129
 
04. Ruby Operators Slides - Ruby Core Teaching
04. Ruby Operators Slides - Ruby Core Teaching
quanhoangd129
 
08. Ruby Enumerable - Ruby Core Teaching
08. Ruby Enumerable - Ruby Core Teaching
quanhoangd129
 
01. Ruby Introduction - Ruby Core Teaching
01. Ruby Introduction - Ruby Core Teaching
quanhoangd129
 
09. Ruby Object Oriented Programming - Ruby Core Teaching
09. Ruby Object Oriented Programming - Ruby Core Teaching
quanhoangd129
 
07. Ruby String Slides - Ruby Core Teaching
07. Ruby String Slides - Ruby Core Teaching
quanhoangd129
 
06. Ruby Array & Hash - Ruby Core Teaching
06. Ruby Array & Hash - Ruby Core Teaching
quanhoangd129
 
05. Ruby Control Structures - Ruby Core Teaching
05. Ruby Control Structures - Ruby Core Teaching
quanhoangd129
 
04. Ruby Operators Slides - Ruby Core Teaching
04. Ruby Operators Slides - Ruby Core Teaching
quanhoangd129
 
08. Ruby Enumerable - Ruby Core Teaching
08. Ruby Enumerable - Ruby Core Teaching
quanhoangd129
 
Ad

Recently uploaded (20)

Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
 
Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
 
SAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.ppt
MuhammadShaheryar36
 
Generative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its Applications
SandeepKS52
 
Transmission Media. (Computer Networks)
Transmission Media. (Computer Networks)
S Pranav (Deepu)
 
Advanced Token Development - Decentralized Innovation
Advanced Token Development - Decentralized Innovation
arohisinghas720
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
Software Testing & it’s types (DevOps)
Software Testing & it’s types (DevOps)
S Pranav (Deepu)
 
What is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdf
Varsha Nayak
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
 
AI-Powered Compliance Solutions for Global Regulations | Certivo
AI-Powered Compliance Solutions for Global Regulations | Certivo
certivoai
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
 
Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
 
SAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.ppt
MuhammadShaheryar36
 
Generative Artificial Intelligence and its Applications
Generative Artificial Intelligence and its Applications
SandeepKS52
 
Transmission Media. (Computer Networks)
Transmission Media. (Computer Networks)
S Pranav (Deepu)
 
Advanced Token Development - Decentralized Innovation
Advanced Token Development - Decentralized Innovation
arohisinghas720
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
How to Choose the Right Web Development Agency.pdf
How to Choose the Right Web Development Agency.pdf
Creative Fosters
 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
Software Testing & it’s types (DevOps)
Software Testing & it’s types (DevOps)
S Pranav (Deepu)
 
What is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdf
Varsha Nayak
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
 
AI-Powered Compliance Solutions for Global Regulations | Certivo
AI-Powered Compliance Solutions for Global Regulations | Certivo
certivoai
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Ad

03. Ruby Variables & Regex - Ruby Core Teaching

  • 2. Contents VARIABLE NAMING RULES LOCAL VARIABLES INSTANCE, CLASS VARIABLES SIGIL GLOBAL VARIABLES ENVIRONMENT & COMMAND-LINE VARIABLES PSEUDO REGULAR EXPRESSION
  • 4. Variable naming rules A variable is a place to store data. Each variable is given a unique name. There are some naming conventions which apply to variable names. Ruby belongs to the family of dynamic languages. Unlike strongly typed languages like Java, C or Pascal, dynamic languages do not declare a variable to be of certain data type. Instead of that, the interpreter determines the data type at the moment of the assignment. Variables in Ruby can contain different values and different types of values over time.
  • 5. Variable naming rules Ruby is a case sensitive language. It means that age and Age are two different variable names. Most languages are case sensitive. While we can create different names by changing the case of the characters, this practice is not recommended.
  • 6. Variable naming rules Variable names in Ruby can be created from alphanumeric characters and the underscore _ character. A variable cannot begin with a number. This makes it easier for the interpreter to distinguish a literal number from a variable. Variable names cannot begin with a capital letter. If an identifier begins with a capital letter, it is considered to be a constant in Ruby.
  • 7. Variable naming rules The script shows three descriptive variable names. The place_of_birth is more descriptive to a programmer than e.g. pob. It is generally considered OK to choose simple variable names in loops.
  • 9. Sigils Variable identifiers can start with special characters also called sigils. A sigil is a symbol attached to an identifier. Variable sigils in Ruby denote variable scope. The Ruby variable sigils are $ and @.
  • 11. Local variable Local variables are variables that are valid within a local area of a Ruby source code. This area is also referred to as local scope. Local variables exist within the definition of a Ruby module, method, class. If we access the variable outside the scope of the method, Ruby will issue a NameError because Ruby does not find the variable.
  • 14. Global variables Global variables are valid everywhere in the code. They start with a $ sigil in Ruby. The use of global variables is discouraged. Global variables easily lead to many programming errors. Global variables should be used only when there is a reason to do so. Instead of global variables, programmers are advised to use local variables whenever possible.
  • 17. Instance & class variables In this section we will briefly cover instance and class variables. They will be described in Object-oriented programming chapter in more detail. Instance variables are variables that belong to a particular object instance. Each object has its own object variables. Instance variables start with a @ sigil. Class variables belong to a specific class. All objects created from a particular class share class variables. Class variables start with @@ characters.
  • 18. Instance & class variables
  • 20. Environment & command- line variables The ENV constant gives access to environment variables. It is a Ruby hash. Each environment variable is a key to the ENV hash. The ARGV constant holds command-line argument values. They are passed by the programmer when the script is launched. The ARGV is an array that stores the arguments as strings. The $* is an alias to the ARGV. Both ENV and ARGV are global constants.
  • 24. Pseudo variables Ruby has a few variables which are called pseudo variables. They are different from regular variables. We cannot assign values to pseudo variables The self is the receiver of the current method. The nil is the sole instance of the NilClass. It represents the absense of a value. The true is the sole instance of the TrueClass. It represents boolean true. The false is a sole instance of FalseClass. It represents boolean false.
  • 26. RegEx A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings using a specialized syntax held in a pattern.