Skip to content

Instantly share code, notes, and snippets.

@simcap
Last active August 29, 2015 14:06
Show Gist options
  • Save simcap/4ec61349400c4bde3820 to your computer and use it in GitHub Desktop.
Save simcap/4ec61349400c4bde3820 to your computer and use it in GitHub Desktop.
A good intermediary for internal only access of instance variables
# 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
@gabrielnau
Copy link

@simcap not sure to understand, would you mind looking at this tomorrow together ?

@simcap
Copy link
Author

simcap commented Sep 3, 2014

of course ...

@Igosuki
Copy link

Igosuki commented Sep 3, 2014

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

@Igosuki
Copy link

Igosuki commented Sep 3, 2014

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