Scott
3/19/2006 8:43:00 PM
In class B, you are attepmting to use an instance variable as if it
were a class variable. To set @blah to a value when its instantiated,
you'll need to stick that line in the constructor:
class B
include A
extend A
def initialize
@blah = 'YES!'
end
end
Also, you dont need to both include and extend A inside B. Use extend
when you want to use the modules methods as class methods:
module X
def classy
puts "Yup, I'm classy"
end
end
class Y
extend X
end
Y.classy
=> Yup, I'm classy
You can also extend an instance of an object:
s = "Holy shniekes"
s.extend(X)
s.classy
=> Yup, I'm classy
Use include when you want to "mix-in" the modules methods with the
instance methods defined in the class:
module X
def classy
puts "Yup, I'm classy"
end
end
class Y
include X
end
Y.new.classy
=> Yup, I'm classy
Hope that helps :)
- Scott