Created
October 8, 2015 05:24
-
-
Save beaugaines/039d3984763ea0787146 to your computer and use it in GitHub Desktop.
Initialize vs attr_accessor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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