Un Punto Mutable

[~/rubytesting/TheRubyProgrammingLanguage/Chapter7ClassesAndModules/point]$ cat -n mutable_point.rb 
     1  #encoding : utf-8
     2  class MutablePoint
     3    def initialize(ñ,y)
     4     @ñ, @y = ñ, y 
     5    end
     6  
     7    def ñ; @ñ; end       # The getter method for @ñ
     8    def y; @y; end       # The getter method for @y
     9  
    10     def ñ=(value)        # The setter method for @ñ
    11       @ñ = value
    12     end
    13  
    14    def y=(value)        # The setter method for @y
    15      @y = value
    16    end
    17  
    18     def add!(p)          # Add p to self, return modified self
    19       @ñ += p.ñ
    20       @y += p.y
    21       self
    22     end
    23  #  ---------------------------
    24    def add(p)           # A nonmutating version of add!
    25      q = self.dup       # Make a copy of self
    26      q.add!(p)          # Invoke the mutating method on the copy
    27    end
    28  
    29  end
    30  
    31  if __FILE__ == $0
    32    z = MutablePoint.new(1,2)
    33    p z
    34    t = MutablePoint.new(2,1)
    35    p t 
    36    t.ñ = 4
    37    p t 
    38    z.add!(t)
    39    p z
    40  end



Casiano Rodriguez León 2015-01-07