On Sat, Oct 27, 2012 at 10:49 PM, Igor Pirnovar <[email protected]> wrote:
> Robert Klemme wrote in post #1081589:
>
>>>   s = S.new(0)
>>>   p s.a    #=> 10 WRONG!!!
>>>   s.a = 100
>>>   p s.a    #=> 105
>>
>> Igor, that was not my code.  Your claim is wrong.  I posted this
>> upthread:
>
> I consider this red herring, and in continuation you actually repeat,
> your half-way solution in which 'initialize' is using straight
> assignment rather than expression 'n + 5'! You again ignore the
> requirement that both 'initialize' method and the accessor (setter)
> method initialize the variable in identical fashion!

Can we agree that if I ignore your requirement to initialize the
variable in identical fashion this is what will happen: the value
assigned in the constructor is modified in a different way than the
value assigned via the attribute writer method resulting in different
values of the property from #initialize and from assignment?  OK.  Now
with my code this is what happens:

irb(main):001:0> S = Struct.new :num do
irb(main):002:1* alias _initialize initialize
irb(main):003:1> def initialize(n)
irb(main):004:2> super
irb(main):005:2> self.num = n
irb(main):006:2> end
irb(main):007:1> alias _num= num=
irb(main):008:1* def num=(n) self._num= n + 5 end
irb(main):009:1> end
=> S
irb(main):010:0> s = S.new 0
=> #<struct S num=5>
irb(main):011:0> s.num
=> 5
irb(main):012:0> s.num == 0 + 5
=> true
irb(main):013:0> s.num = 10
=> 10
irb(main):014:0> s.num
=> 15
irb(main):015:0> s.num == 10 + 5
=> true
irb(main):016:0> s.num = 0
=> 0
irb(main):017:0> s.num
=> 5
irb(main):018:0> s.num == 0 + 5
=> true
irb(main):019:0> s = S.new 10
=> #<struct S num=15>
irb(main):020:0> v1 = s.num
=> 15
irb(main):021:0> s = S.new 0
=> #<struct S num=5>
irb(main):022:0> s.num = 10
=> 10
irb(main):023:0> v2 = s.num
=> 15
irb(main):024:0> v1 == v2
=> true

To me this looks suspiciously like #initialize and assignment apply
the same modification of the value.  Apparently I'm wrong - but where?

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- You received this message because you are subscribed to the Google Groups 
ruby-talk-google group. To post to this group, send email to 
[email protected]. To unsubscribe from this group, send email 
to [email protected]. For more options, visit this 
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en

Reply via email to