-
-
Save shime/3029643 to your computer and use it in GitHub Desktop.
difference between using @ over self
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 age | |
@age | |
end | |
def initialize(age) | |
@age = age | |
end | |
end | |
d = Dog.new(8) | |
puts d.age |
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 | |
attr_reader :age | |
def initialize(age) | |
@age = age | |
end | |
end | |
d = Dog.new(8) | |
puts d.age |
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 age | |
@age | |
end | |
def age= age | |
@age = age | |
end | |
def initialize(age) | |
self.age = age | |
end | |
end | |
d = Dog.new(8) | |
puts d.age |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment