Skip to content

Instantly share code, notes, and snippets.

@beaugaines
Created October 8, 2015 05:24
Show Gist options
  • Select an option

  • Save beaugaines/039d3984763ea0787146 to your computer and use it in GitHub Desktop.

Select an option

Save beaugaines/039d3984763ea0787146 to your computer and use it in GitHub Desktop.
Initialize vs attr_accessor
class Dog
def initialize(breed, color)
@breed, @color = breed, color
end
def describe_me
"I am a #{@breed} and I am #{@color}"
end
end
class Monkey
attr_accessor :variety, :smarts
end
d = Dog.new('Dalmatian', 'mottled')
d.describe_me # "I am a Dalmatian and I am mottled"
d.breed # error
d.breed = 'Collie' # error
d.color # error
d.color = 'Red' # error
m = Monkey.new('Great ape', 'Smart') # error
m = Monkey.new
m.variety # error
m.smarts # error
m.variety = 'Great ape'
m.variety # 'Great ape'
m.smarts = 'Smart'
m.smart # 'Smart'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment