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

Intro to Ruby Variables

  • 1.
    RUBY – variabletypes, brief intro
  • 2.
    Variables? ● ● ● ● Most of youknow 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 AboutRuby 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 isa 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 variablesare 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.
  • 6.
  • 7.
    Class Vars ● ● ● ● Class variablesstart 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.
  • 8.
  • 9.
    Instance Vars ● ● ● Instance variablesstart 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.
  • 10.
  • 11.
    Local Vars ● ● ● Local variablesdo 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.
  • 12.
  • 13.
    Constant Vars ● ● ● ● ● Constant varsdo 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.
  • 14.
  • 15.
    Pseudo-variables ● Special variables thathave the appearance of local variables but behave like constants. You can not assign any value to these variables.
  • 16.
    Pseudo-var Examples ● self - currentobject ● 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
  • 17.