Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

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
Comment

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
Comment

What is attr_writer 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"
Comment

PREVIOUS NEXT
Code Example
Ruby :: rails migration remove column 
Ruby :: rails controller generator 
Ruby :: including libraries in ruby 
Ruby :: ruby if statement multiple conditions 
Ruby :: generate view rails 
Ruby :: ruby includes 
Ruby :: rails duplicate record 
Ruby :: ruby get min value from array 
Ruby :: rails api 
Ruby :: online ruby compiler 
Ruby :: logstasher-logger gem 
Ruby :: ruby rspec change matcher 
Ruby :: rails scope where not 
Ruby :: ruby pdf to file 
Ruby :: rails deliver_later with delay 
Ruby :: ruby create object with attributes 
Ruby :: ruby find lower number array object 
Ruby :: capybara click with offset 
Ruby :: height of a tree in ruby 
Ruby :: ruby read file line by line 
R :: r count number of na 
R :: r loops 
R :: r test normality 
R :: remove all empty strings from R 
R :: r dataframe append row 
R :: how to read in txt/csv files into r 
R :: convert index to column r 
R :: how to get quantile summary statistics in r summarise 
R :: change the y ticks in r plot 
R :: copy list R 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =