oop - Understanding Ruby Classes and Modules -
i have number of questions regarding ruby classes , modules. have written number of test files kind of explore them further long post here (https://github.com/senjai/learning-ruby/blob/master/class_test.rb , https://github.com/senjai/learning-ruby/blob/master/mixin-instance-var-conflict.rb)
first 3 questions variables.
so far understand it, instance variables prefixed @, , unique each instance of class. can accessed accessor methods (like attr_accessor)
second, class variables, they're prefixed @@. how come class variables can't used attr_accessor/reader? what's difference between class.class_variable , instance.class_variable? if instance modifies class variable within class (e.g in instance method) affect static variable in class definition?
thirdly,
why instance/class variables need prefixed @? happens when aren't prefixed @ all? in example below:
module test state = {} def state=(value) state[object_id] = value end def state state[object_id] end end
for class methods, understand instance methods defined normal functions, , class methods have prefixed self. why can't use class methods in instantiated object? why self make special?
lastly, scope resolution operator, i've found works constants , class methods, what's point of having scope resolution operator in first place if can accessed in dot notation?
Comments
Post a Comment