Last active
August 29, 2015 14:06
-
-
Save simcap/4ec61349400c4bde3820 to your computer and use it in GitHub Desktop.
A good intermediary for internal only access of instance variables
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
# A good intermediary for internal-only access of instance variables | |
# Here the example is with the access on the 'sauce' instance variable | |
class Salad | |
def initialize(sauce) | |
@sauce = sauce | |
end | |
attr_reader :sauce | |
private :sauce | |
def prepare | |
pour(sauce) # sauce can now be privately used with an attr_reader | |
end | |
end | |
# Instead of | |
class Salad | |
def initialize(sauce) | |
@sauce = sauce | |
end | |
def prepare | |
pour(@sauce) # @sauce can be privately used but typos will not raised | |
end | |
end |
is this just a best practices/syntax/teamwork kind of thing or am I missing something ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
private :sauce raises an error if :sauce is not defined, which does not prevent use of instance variables but makes sure the code breaks if accessors aren't defined