All the Variables of Ruby
In ruby there are six different types of variables. Some very obscure and rarely used, but interesting to know.
Used in the scope of a method or other local usage.
name = 'stuart'
Usually used as a value that is set once to set a default or reusable value. They use all caps as a convention.
class Person TYPES = [:admin, :user].freeze # hint use the freeze method to make constants immutable end
Saves a values to the instance of a class.
def name=(name) @name = name end
Saves a value to the class itself. Works almost like a class constant.
def self.count_plus_one @@count += 1 end
A variable that can be accessed anywhere from within the program. Admittedly I have never seen these used. Perhaps it makes more sense to make a class, nevertheless it's worth mentioning.
$access_me_anywhere = 42