Created
January 13, 2016 16:53
-
-
Save sashaegorov/45d99a05782018ef0dc4 to your computer and use it in GitHub Desktop.
Ruby private inheritance
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 Parent | |
attr_accessor :a, :b | |
def initialize(a) | |
@a = a | |
end | |
private | |
def set_b | |
@b = 'b' | |
end | |
end | |
class Child < Parent | |
attr_accessor :c | |
def initialize(c) | |
@c = c | |
end | |
def set_c | |
set_b | |
end | |
end | |
c = Child.new('x') | |
# => #<Child:0x007f9989919680 @c="x"> | |
c.a | |
# => nil | |
c.b | |
# => nil | |
c.c | |
# => "x" | |
c.set_c | |
# => "b" | |
c.a | |
# => nil | |
c.b | |
# => "b" | |
c.c | |
# => "x" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment