Created
July 16, 2014 10:11
-
-
Save mkwiatkowski/bc6ddc5afd72390a065f to your computer and use it in GitHub Desktop.
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 Thing | |
def description | |
# An object can always access its methods, whether they are public, | |
# protected or private. | |
"#{shape} thing in #{color}" | |
end | |
def combined_description(other) | |
# An object can access methods of other objects of the same class if they | |
# are public or protected. | |
# Here other.shape would cause an error, because it's private. | |
# But other.color can be called, because it's protected and the other is | |
# also of class Thing. | |
"#{shape} thing in #{color} and the other thing in #{other.color}" | |
end | |
def numbered_description(number) | |
# Here, number is a Fixnum (different class than Thing), but we can call its | |
# to_s method, because it's public. | |
"#{number.to_s} #{shape} things in #{color}" | |
end | |
protected | |
def color | |
"red" | |
end | |
private | |
def shape | |
"square" | |
end | |
end | |
thing = Thing.new | |
puts thing.description | |
puts thing.combined_description(Thing.new) | |
puts thing.numbered_description(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment