[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Ruby and OO (fwd)



> class SomeObject
>   attr_reader :memberVar
>   attr_writer :memberVar
>
>   def initialize()
>     @memberVar = 10
>   end
>
>   def isMemberVarBig?
>     return @memberVar > 100
>   end
> end
>
> attr_read/writer is an easier way of defining getters/setters. the
> second method is the same as a getter, it just illustrates using the
> member variable (you don't need to write the return because the
> value of the last expression executed is always returned).

Oops. the second method is not the same as a getter (i changed it
after typing). the getter is used as follows

s = SomeObject.new
puts s.memberVar

the second method could be used as

if s.isMemberVarBig? then puts s.memberVar