rails attr_reader code example
Example 1: attr_accessor ruby
class Food
attr_accessor :protein
def initialize(protein)
@protein = protein
end
end
Example 2: ruby attr_writer example
class Person
attr_reader :name, :age, :sex, :email
attr_writer :name, :age, :sex, :email
def initialize(name)
@name = name
end
end
Example 3: ruby attr_writer example
class Person
attr_reader :name
attr_writer :name
def initialize(name)
@name = name
end
end
john = Person.new("John")
john.name = "Jim"
puts john.name # => Jim