Skip to content

Instantly share code, notes, and snippets.

@fr33z3
Last active August 29, 2015 14:05
Show Gist options
  • Save fr33z3/9d432f579b5f5935569d to your computer and use it in GitHub Desktop.
Save fr33z3/9d432f579b5f5935569d to your computer and use it in GitHub Desktop.
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