view · edit · sidebar · attach · print · history

Ruby set and get methods

<< Require | Index | Scope >>

class Test        #get method
 def masa
   @aaa
 end
 def masa=(val)   #set method
   @aaa = val
 end
end

# is the same as above, 'set' and 'get' combined

class Test2
 attr_accessor :masa
end

test = Test.new
test.masa = 123
p test.masa

test2 = Test2.new
test2.masa = 223
p test2.masa
view · edit · sidebar · attach · print · history
Page last modified on April 07, 2011, at 04:31 PM