Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

What is attr_accessor in Ruby?

# What is attr_accessor, attr_reader or attr_writer in Ruby?

# Verbose (hand written reader and writer)
class Person
  def name
    @name
  end
  def name=(str)
    @name = str
  end
end

# or using attr_reader and/or attr_writer
class Person
  attr_reader :name
  attr_writer :name
end

# or using attr_accessor
class Person
  attr_accessor :name
end

# Usage
person = Person.new
person.name = 'Dennis'
person.name # => "Dennis"
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #What
ADD COMMENT
Topic
Name
5+4 =