From: Josh Cheek Date: 2013-04-01T04:20:01+09:00 Subject: Re: Private setters can be called by self, why not getters? --047d7b3432fae1807c04d93d6309 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Mar 31, 2013 at 1:19 PM, Igor Pirnovar wrote: > You have to define setters and getters in ruby more carefully, when you > intend to use them as private methods! You have to separate what is > getter and what is setter, especially, when one and the same symbol can > be used for both operations simultaneously as is the case in combined > assignment operators such as +=, -=, *=, /= ... > > > Here is how you manage the getters and setters when setter but not > getter should be private: > > --------------------------------------------------- > #!/usr/bin/env ruby > class Counter > attr_reader :count > attr_writer :count > private :count= > > def initialize; self.count = 0; end > def show_count; puts "Count=#{count}"; end > def increment1; self.count = count + 1; end > def increment2; self.count = self.count + 1; end > def increment3; self.count += 1; rescue; end > end > > o = Counter.new > o.show_count > o.increment1 > o.show_count #=> Count=0 > o.increment2 > o.show_count #=> Count=1 > o.increment3 > o.show_count #=> Count=2 > > puts "Explicit get: Count=#{o.count}" #=> Explicit get: Count=2 > o.count=1234 > ---------------------------------------------------- > > Indeed, if you do not care or are ignorant of oo, you can resort to > devices like the underscore as suggested above. > > I rarely want to expose either. In my code example, why can I call `self.count=` but not `self.count` ? --047d7b3432fae1807c04d93d6309 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Sun, Mar 31, 2013 at 1:19 PM, Igor Pirnovar <lists@ruby-forum.com> wrote:
You have to define setters and getters in ruby more carefully, when you
intend to use them as private methods! You have to separate what is
getter and what is setter, especially, when one and the same symbol can
be used for both operations simultaneously as is the case in combined
assignment operators such as +=3D, -=3D, *=3D, /=3D ...


Here is how you manage the getters and setters when setter but not
getter should be private:

=A0 ---------------------------------------------------
=A0 #!/usr/bin/env ruby
=A0 class Counter
=A0 =A0 attr_reader :count
=A0 =A0 attr_writer :count
=A0 =A0 private :count=3D

=A0 =A0 def initialize; self.count =3D 0; end
=A0 =A0 def show_count; puts "Count=3D#{count}"; end
=A0 =A0 def increment1; self.count =3D count + 1; end
=A0 =A0 def increment2; self.count =3D self.count + 1; end
=A0 =A0 def increment3; self.count +=3D 1; rescue; end
=A0 end

=A0 o =3D Counter.new
=A0 o.show_count
=A0 o.increment1
=A0 o.show_count =A0#=3D> Count=3D0
=A0 o.increment2
=A0 o.show_count =A0#=3D> Count=3D1
=A0 o.increment3
=A0 o.show_count =A0#=3D> Count=3D2

=A0 puts "Explicit get: Count=3D#{o.count}" =A0#=3D> Explicit = get: Count=3D2
=A0 o.count=3D1234
=A0 ----------------------------------------------------

Indeed, if you do not care or are ignorant of oo, you can resort to
devices like the underscore as suggested above.


I rarely want to expose either. In my code example, = why can I call `self.count=3D` but not `self.count` ?
--047d7b3432fae1807c04d93d6309--