Pages

Saturday, August 06, 2011

ruby - the deal with variables

Here's a sample scenario:
Base class: Product
Sub-classes that inherit from base class: Electronic, Clothing, MusicalInstrument, Food


local variables

  • does not get set to nil prior to initialisation
  • to declare one, just put a underscore in front of the name OR just the name itself (no "@" or "@@"). 
  • they come to live only when they are assigned with a value (initialisation)
  • its scope is limited to the block which it is in
  • helper method: defined?( expression/argument ). Thanks to Shane Hanna for passing me this link for a better understanding of the method :)
    • it will return the type of expression or argument  is otherwise the value, nil. 
      p defined?( exchange_rate = 2.5; end )
          >> expression

    • if given an expression, please note that defined DOES NOT RUN the expression. It just tries to figure out what it actually is
      p defined?(exhange_rate = 2.5); end)
          >> expression
          p exchange_rate
          >> nil
                   - the reason why this is returning nil is because exchange_rate was a local variable within the expression but given that defined never runs any expression, the value of the 2.5 was in the scope of the defined expression only
    • the argument is usually a single identifier. For example, a variable 
      def store_name; end;
          puts defined.store_name;
          >> "method"


instance variables

  • gets set to nil before it's used for the first time
  • i would call them "private" or "attribute variables" of a class
  • if you've done C++ or java, think of instance variables as variables with a private scope for which class accessor and mutator methods are used to interact with these variables (encapsulation virtue of object oriented programming)
  • example: name of each Electronic, MusicalInstrument, Clothing, Food class. Say if stratocaster and telecaster were objects which instantiated from the Electronic class, its name and description attributes are instance variables. ** Telecaster and stratocasters are musical instruments
  • Reference: http://www.rubyist.net/~slagell/ruby/instancevars.html

class variables

  • gets set to nil before it's used for the first time
  • scope is limited to the class and all its instances hence they are shared between all instances of the class
  • to declare one, just prepend a "@@" symbol in front of the variable's name
  • example: @@government_tax variable that is the same across all products being sold in a store.  For Australia, the government tax (aka gst) is 10% across the board


global variables
constants
  • begins with a letter within A-Z
  • it can be reassigned but ruby will issue a warning. In perl, doing so is impossible as the interpreter restricts further assignments after the initial assignment

Variable names - ruby 1.9 and above now support utf 8

  • this means that we are no longer restricted to a-zA-Z characters for variable names
  • one way to explicitly have ruby select utf-8 for encoding is to have the comment, "# encoding: utf-8" at the top of the script (usually after "#!/usr/bin/ruby")
  • prior to ruby 1.9, local variable names have to begin with a letter within a-z
  • with ruby 1.9 onwards, utf 8 encoding is supported
  • Credits to  Shane Hanna's example and Ruby 1.9's Three Default Encodings

Good reference(s):