Trying to understand Ruby's getter setter rules -
what can't understand
def initialize(awesome_level2) @awesome_level2 = awesome_level2
does @awesome_level2 have have same name awesome_level2?
it seems if change it, doesn't work properly
class awesome attr_accessor :awesome_level2 def initialize(awesome_level2) p awesome_level2 #100 @awesome_level2 = awesome_level2 p @awesome_level2 #100 end end awesome_sauce = awesome.new(100) puts awesome_sauce.awesome_level2 #100 awesome_level2 from? awesome_sauce.awesome_level = 99 puts awesome_sauce.awesome_level #99
attr_accessor
convenience method creates getter , setter methods symbols pass arguments. can access instance variable creates directly, in case @awesome_level2
. note has @
symbol in front of it, indicates it's instance variable. different local variable or method parameter not have @ sign. therefore in initialize method, variable names @awesome_list2
, awesome_list2
different , can hold different values.
as @prescott has said, seems though trying set instance variable @awesome_list2
argument passed initializer, awesome_list2
, accidentally wrote capital a, going set nil since object name awesome_list2
(note capital 'a') doesn't exist (i presume).
Comments
Post a Comment