Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

attr_accessor ruby

class Food
 attr_accessor :protein
 def initialize(protein)
   @protein = protein
 end
end
Comment

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"
Comment

PREVIOUS NEXT
Code Example
Ruby :: ruby loop through array 
Ruby :: rails disable submit button 
Ruby :: linked list in ruby 
Ruby :: super vs super() ruby 
Ruby :: rails validates_presence_of 
Ruby :: ruby create engine using rspec and dummy 
Ruby :: rails reference a column with another name 
Ruby :: rails secure uuid 
Ruby :: rails keep all params except for some 
Ruby :: rails server stop pid 
Ruby :: What does inject in ruby do 
Ruby :: Rails is not defined 
Ruby :: ruby raise to power 
Ruby :: .delete ruby 
Ruby :: ruby abs function programming 
Ruby :: ruby array of strings 
Ruby :: ide for ruby 
Ruby :: ruby merge arrays unique 
Ruby :: run method before rails 
Ruby :: why are getters and setters important ruby 
Ruby :: ruby on rails recover data in params with form tag 
Ruby :: ruby puts object 
Ruby :: og meta tags not working rails 
Ruby :: rails rails admin secure page 
Ruby :: ruby read file line by line 
R :: how to set a column as index r 
R :: make a sequence of timeseries per day in r 
R :: fourier in R 
R :: How to Convert a Factor in R 
R :: r: rename a column 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =