Ruby??™s functions for accessing instance variables
and class variables check to ensure that the names passed are in the proper format:
A.instance_variable_get(:@@var)
# ~> -:17:in `instance_variable_get': `@@var' is not allowed as an instance
variable name (NameError)
* There are also constants, but they shouldn??™t vary. (They can, but Ruby will complain.)
20 | Chapter 1: Foundational Techniques
Class variables can be somewhat confusing to use. They are shared all the way down
the inheritance hierarchy, so subclasses that modify a class variable will modify the
parent??™s class variable as well.
>> class A; @@x = 3 end
=> 3
>> class B < A; @@x = 4 end
=> 4
>> class A; @@x end
=> 4
This may be useful, but it may also be confusing. Generally, you either want class
instance variables??”which are independent of the inheritance hierarchy??”or the
class inheritable attributes provided by ActiveSupport, which propagate values in
a controlled, well-defined manner.
Blocks, Methods, and Procs
One powerful feature of Ruby is the ability to work with pieces of code as objects.
There are three classes that come into play, as follows:
Proc
A Proc represents a code block: a piece of code that can be called with arguments
and has a return value.
UnboundMethod
This is similar to a Proc; it represents an instance method of a particular class.
(Remember that class methods are instance methods of a class object, so
UnboundMethods can represent class methods, too.
Pages:
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46