On Thu, Oct 25, 2012 at 6:25 PM, Igor Pirnovar <[email protected]> wrote:
> Robert Klemme wrote in post #1081146:
>
>> I strongly object. Struct is a very useful tool I use all the time to
>> define data containers quickly.
>
>>> The above problem can be solved if you use accessor methods in place of
>>> '@' variable:
>>
>> Exactly.
>
> I have no objection if anybody wishes to use the broken Struct as it is
It's not broken. It works like designed.
> in his/her own short-cuts and sketches. However, if allowed there, who
> is going to prevent its use in main-stream programming?
Nobody, why should they?
> The trouble is,
> this stuff is so broken in situations becomes totally useless. Its
> inconsistencies with regular ruby classes make things unacceptable on
> the first place, and I can not believe anyone would defend its
> continuous usage in this broken fashion! If it is so bloody useful, fix
> the darn thing so it doesn't introduce unnecessary exceptions to the
> impeccable consistent and beautiful Ruby oo language!
You are repeating that it is broken but you fail to explain what
exactly is broken. That's not a basis for discussions.
> Try to fix the following without redefining the redefining accessor for
> 'num' instance variable within the class definition hence invalidating
> the use of Struct:
>
> S = Struct.new(:num)
> class S
> attr_accessor :num
> def initialize(n); @num=n+5; end
> def num=(n); @num = n+5; end
> end
>
> s = S.new(3)
> puts s.num #=> 8
> s.num = 4
> puts s.num #=> 9
Easy:
S = Struct.new :num do
alias _initialize initialize
def initialize(n)
super
self.num = n
end
alias _num= num=
def num=(n) self._num= n + 5 end
end
s = S.new 0
p s.num
Btw, having an accessor that changes the value in this way is probably
a questionable practice in itself.
Cheers
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