Skip to content

Instantly share code, notes, and snippets.

@giwa
Last active February 18, 2016 04:26
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 giwa/3e028d6c0eb2fdc00d2b to your computer and use it in GitHub Desktop.
Save giwa/3e028d6c0eb2fdc00d2b to your computer and use it in GitHub Desktop.
ER 6 Know How Ruby Build Inheritance Hierarachies ref: http://qiita.com/giwa/items/2390a3f93a9e8c474707
BasicObject
<- Kernel (module)
<- PP::ObjectMixin(module)
<- Object
<- Person
<- Customer
[3] pry(main)> class Person
[3] pry(main)* def hi
[3] pry(main)* puts 'hi'
[3] pry(main)* end
[3] pry(main)* end
[4] pry(main)> class Customer < Person
[4] pry(main)* end
[11] pry(main)> Customer.superclass
=> Person
[12] pry(main)> Customer.singleton_class
=> #<Class:Customer>
[13] pry(main)> Customer.ancestors
=> [Customer, Person, Object, PP::ObjectMixin, Kernel, BasicObject]
[14] pry(main)> Customer.included_modules
=> [PP::ObjectMixin, Kernel]
[16] pry(main)> module PersonMixin
[16] pry(main)* def name
[16] pry(main)* puts 'foo'
[16] pry(main)* end
[16] pry(main)* end
[18] pry(main)> Person.superclass
=> Object
[19] pry(main)> Customer.ancestors
=> [Customer, Person, PersonMixin, Object, PP::ObjectMixin, Kernel, BasicObject]
[20] pry(main)> module PersonMixin2
[20] pry(main)* def name
[20] pry(main)* puts 'bar'
[20] pry(main)* end
[20] pry(main)* end
=> :name
[21] pry(main)> class Person
[21] pry(main)* include PersonMixin
[21] pry(main)* include PersonMixin2
[21] pry(main)* end
=> Person
[22] pry(main)> Customer.ancestors
=> [Customer,
Person,
PersonMixin2,
PersonMixin,
Object,
PP::ObjectMixin,
Kernel,
BasicObject]
[28] pry(main)> Customer.superclass
=> Person
[29] pry(main)> Customer.superclass.superclass
=> Object
[30] pry(main)> Customer.superclass.superclass.superclass
=> BasicObject
[31] pry(main)> Customer.included_modules
=> [PersonMixin2, PersonMixin, PP::ObjectMixin, Kernel]
[37] pry(main)> customer = Customer.new
=> #<Customer:0x007f9de428f008>
[38] pry(main)> def customer.name
[38] pry(main)* "baz"
[38] pry(main)* end
[42] pry(main)> customer.singleton_methods
=> [:name]
[43] pry(main)> customer.name
=> "baz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment