Showing posts with label variables. Show all posts
Showing posts with label variables. Show all posts

Saturday, March 28, 2009

Variables are created and set to nil as soon as they are mentioned

Tim:~> irb
>> local_variables
=> ["_"]
>> if false
>> some_variable = :yahoo
>> end
=> nil
>> some_variable
=> nil
>> local_variables
=> ["_", "some_variable"]

Isn't that interesting? When I define a variable within an if statement that will never be entered (and thus the variable will never be assigned to :yahoo), it is listed as a local variable and set to nil.


Saturday, March 21, 2009

Ruby Variables

  • $global variables are available from anywhere in the program--no matter the object or method, $global will be available.
  • $local variables are only available with the limited context in which they are defined--within a method or block, or if defined outside of a method or block, within whatever self is (a class or module or main).
  • @instance variables are available anywhere within a specific instance of an object.
  • @@class variables are common to all instances of a class. They can be used to count the instance of a class, as shown in the figure above.