class - Modules and classes in Ruby. Contradictions? -
according rubymonk section 8.1 modules hold behavior , not state , classes can hold behavior , state.
yet modules super-class of classes in ruby. how can be?
oh brother, , if forget module/class instance variables , module/class methods, can't classes hold state--because it's instances of classes hold state. classes hold list of instance methods. whole section on classes technically wrong too.
the bottom line 99.99% of things in ruby objects, , object can hold state. class object(as producer of objects), module object(but not producer of objects), , instances of classes objects.
i suggest not worry state. concentrate on fact modules can used 2 things:
1) namespace:
module myfunctions def myfunctions.puts(str) #...or: def self.puts(str) kernel.puts "***" + str end end puts 'hello' myfunctions.puts 'hello' --output:-- hello ***hello
2) package of methods included, e.g. in class:
module animaltricks def speak puts @noise end end class dog include animaltricks def initialize @noise = "woof" end end class mouse include animaltricks def initialize @noise = "sqeak" end end dog.new.speak mouse.new.speak --output:-- woof sqeak
Comments
Post a Comment