Ruby custom and automatic attribute accessors

The automatic one:

class Foo
  attr_accessor :name
 
  def initialize
    @name = "no name"
  end
end


The custom one:

class Foo
  def initialize
    @name = "no name"
  end

  def name
    @name
  end
 
  def name= value
    @name = value
  end
end



Print | posted @ Saturday, January 7, 2012 10:53 PM

Comments have been closed on this topic.