Last active
August 29, 2015 14:05
-
-
Save fr33z3/9d432f579b5f5935569d 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 A | |
def call_a_with_self | |
self.a | |
end | |
def call_a_without_self | |
a | |
end | |
def call_b_with_self | |
self.b | |
end | |
def call_b_without_self | |
b | |
end | |
private | |
def a | |
puts 'a' | |
end | |
protected | |
def b | |
puts 'b' | |
end | |
end | |
Если вызываем приватный метод с self, то внезапно: | |
> A.new.call_a_with_self | |
=> NoMethodError: private method `a' called for #<A:0x007fc4428ce868> | |
выкидывает эксепшен, при этом вызов без self | |
> A.new.call_a_without_self | |
a | |
распечатает литера 'a' | |
Если при этом вызовем протектед мето с self, то | |
> A.new.call_b_with_self | |
b | |
все нормально вызывается, и даже без self все будет ок | |
> A.new.call_b_without_self | |
b | |
А теперь про наследование этих методов | |
наследуем новый класс и пытаемся вызывать методы | |
class C < A | |
end | |
> C.new.call_a_without_self | |
a | |
> C.new.call_b_without_self | |
b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment