Created
March 4, 2014 06:25
-
-
Save jimmycuadra/9341298 to your computer and use it in GitHub Desktop.
Instance variable access in Ruby
This file contains 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 Person | |
attr_reader :name | |
def initialize(name, age) | |
@name = name | |
@age = age | |
end | |
end | |
ted = Person.new("Ted", 25) | |
ted.name # "Ted" | |
ted.age # NoMethodError: undefined method `age' for #<Person:0x007ffb8a4a12c0 @name="Ted", @age=25> |
This file contains 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 Person | |
def initialize(name, age) | |
@name = name | |
@age = age | |
end | |
def name | |
@name | |
end | |
end | |
ted = Person.new("Ted", 25) | |
ted.name # "Ted" | |
ted.age # NoMethodError: undefined method `age' for #<Person:0x007ffb8a4a12c0 @name="Ted", @age=25> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment