Skip to content

Instantly share code, notes, and snippets.

@sashaegorov
Created January 13, 2016 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sashaegorov/45d99a05782018ef0dc4 to your computer and use it in GitHub Desktop.
Save sashaegorov/45d99a05782018ef0dc4 to your computer and use it in GitHub Desktop.
Ruby private inheritance
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