Skip to content

Instantly share code, notes, and snippets.

@akshaymohite
Created February 7, 2018 12:42
Show Gist options
  • Save akshaymohite/7233a8869764b9065acc5c6814f709bc to your computer and use it in GitHub Desktop.
Save akshaymohite/7233a8869764b9065acc5c6814f709bc to your computer and use it in GitHub Desktop.
delegate_to.rb
2.4.0 :031 > class A
2.4.0 :032?> def initialize
2.4.0 :033?> end
2.4.0 :034?> def a_method
2.4.0 :035?> puts self.class.name
2.4.0 :036?> end
2.4.0 :037?> end
:a_method
2.4.0 :038 > class B
2.4.0 :039?> def initialize
2.4.0 :040?> end
2.4.0 :041?> def b_method
2.4.0 :042?> puts self.class.name
2.4.0 :043?> end
2.4.0 :044?> def a_object
2.4.0 :045?> A.new
2.4.0 :046?> end
2.4.0 :047?> delegate :a_method, to: :a_object
2.4.0 :048?> end
/Users/akshay/.rvm/gems/ruby-2.4.0@eigen/gems/awesome_print-1.7.0/lib/awesome_print/formatters/base_formatter.rb:31: warning: constant ::Fixnum is deprecated
[
[0] :a_method
]
2.4.0 :049 > B
B < Object
2.4.0 :050 > B.new
#<B:0x007fd661710bf8>
2.4.0 :051 > b = B.new
#<B:0x007fd65a460978>
2.4.0 :052 > b.a_method
A
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment