Skip to content

Instantly share code, notes, and snippets.

@kaid
Created March 25, 2011 05:08
Show Gist options
  • Save kaid/886393 to your computer and use it in GitHub Desktop.
Save kaid/886393 to your computer and use it in GitHub Desktop.
illustrating ruby method visibility machanism
class Bla
def a
"a"
end
def b
self.a
end
def c
a
end
end
class Bla0 < Bla
end
b = Bla.new
b0 = Bla0.new
Bla.send(:public, :a)
b.a # => "a"
b.b # => "a"
b.c # => "a"
b0.a # => "a"
b0.b # => "a"
b0.c # => "a"
Bla.send(:protected, :a)
b.a # => NoMethodError
b.b # => "a"
b.c # => "a"
b0.a # => NoMethodError
b0.b # => "a"
b0.c # => "a"
Bla.send(:private, :a)
b.a # => NoMethodError
b.b # => NoMethodError
b.c # => "a"
b0.a # => NoMethodError
b0.b # => NoMethodError
b0.c # => "a"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment