Skip to content

Instantly share code, notes, and snippets.

@sashaegorov
Created January 13, 2016 16:53
Show Gist options
  • Save sashaegorov/45d99a05782018ef0dc4 to your computer and use it in GitHub Desktop.
Save sashaegorov/45d99a05782018ef0dc4 to your computer and use it in GitHub Desktop.

Revisions

  1. sashaegorov created this gist Jan 13, 2016.
    38 changes: 38 additions & 0 deletions ruby_private_inheritance.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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"