Tutorial by Examples

Local variables (unlike the other variable classes) do not have any prefix local_variable = "local" p local_variable # => local Its scope is dependent on where it has been declared, it can not be used outside the "declaration containers" scope. For example, if a local va...
Class variables have a class wide scope, they can be declared anywhere in the class. A variable will be considered a class variable when prefixed with @@ class Dinosaur @@classification = "Like a Reptile, but like a bird" def self.classification @@classification ...
Global variables have a global scope and hence, can be used everywhere. Their scope is not dependent on where they are defined. A variable will be considered global, when prefixed with a $ sign. $i_am_global = "omg" class Dinosaur def instance_method p "global vars ca...
Instance variables have an object wide scope, they can be declared anywhere in the object, however an instance variable declared on class level, will only be visible in the class object. A variable will be considered an instance variable when prefixed with @. Instance variables are used to set and g...

Page 1 of 1