On Tue, Oct 23, 2012 at 7:44 PM, Igor Pirnovar <[email protected]> wrote:
> No doubt, the following code exhibits unacceptable inconsistencies:
>
>   S = Struct.new(:num)
>   class S; def change_to n; @num = n; end; end
>   s = S.new(3)
>   puts s.num     #=> s
>   s.num = 4
>   puts s.num     #=> 4
>   s.change_to(5)
>   puts s.num     #=> 4
>
> Due to the problems shown above, perhaps Struct should be taken out of
> Ruby paradigm all together.

I strongly object.  Struct is a very useful tool I use all the time to
define data containers quickly.

irb(main):001:0> S = Struct.new(:num)
=> S
irb(main):002:0> class S; def change_to(n) self.num=n end end
=> nil
irb(main):003:0> s = S.new 3
=> #<struct S num=3>
irb(main):004:0> s.num
=> 3
irb(main):005:0> s.change_to 123
=> 123
irb(main):006:0> s.num
=> 123

> However, except in limited initialization circumstances, the use of '@'
> variables should be prohibited, and from the point of view of strict
> OOP, it is, by convention!

Right, making using accessor methods a habit is a good idea because it
makes up for more module and thus flexible code.

> The use of 'Struct' should also be
> discouraged, and even worse, mixing Struct with class definitions and
> usage is ill advised.

Sorry, but this is nonsense.  Btw, we can simplify the method definition a bit:

S = Struct.new :num do
  def change_to(n)
    self.num = n
  end
end

> The above problem can be solved if you use accessor methods in place of
> '@' variable:

Exactly.

Kind regards

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