Skip to content

Instantly share code, notes, and snippets.

@MilanGrubnic70
Last active August 29, 2015 14:00
Show Gist options
  • Save MilanGrubnic70/11099782 to your computer and use it in GitHub Desktop.
Save MilanGrubnic70/11099782 to your computer and use it in GitHub Desktop.
Getter & Setter

#Getters & Setters

attr reader :variable1, :variable2, :variable3  
attr_writer :variable1, :variable2, :variable3  
attr_accessor :variable1, :variable2, :variable3

class Person  
  attr_reader :name  
  attr_writer :name  
  def initialize(name)  
    @name = name  
  end  
end

vs.

def name
  @name
end

def name=(value)
  @name = value
end

You're allowed to put an = sign in a method name.
That's just a Ruby convention saying, "hey, this method sets a value!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment