Skip to content

Instantly share code, notes, and snippets.

@jonesdeini
Created September 19, 2012 11:37
Show Gist options
  • Select an option

  • Save jonesdeini/3749204 to your computer and use it in GitHub Desktop.

Select an option

Save jonesdeini/3749204 to your computer and use it in GitHub Desktop.

Revisions

  1. jonesdeini created this gist Sep 19, 2012.
    66 changes: 66 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    class Parent
    def mr_public
    'public'
    end

    def access_other_protected(other)
    other.mr_protected
    end

    def access_other_private(other)
    other.mr_private
    end

    def access_self_protected
    self.mr_protected
    end

    def access_self_private
    self.mr_private
    end

    protected

    def mr_protected
    'protected'
    end

    private

    def mr_private
    'private'
    end
    end

    class Child < Parent
    def access_parent_protected
    mr_protected
    end

    def access_parent_private
    mr_private
    end
    end

    def attempt
    puts yield
    rescue
    puts 'forbidden'
    end

    parent = Parent.new
    other = Parent.new
    child = Child.new

    attempt { parent.mr_public }
    attempt { parent.mr_protected }
    attempt { parent.mr_private }

    attempt { parent.access_self_protected }
    attempt { parent.access_self_private }

    attempt { parent.access_other_protected(other) }
    attempt { parent.access_other_private(other) }

    attempt { child.access_parent_protected }
    attempt { child.access_parent_private }