Skip to content

Instantly share code, notes, and snippets.

@marcosgz
Created July 2, 2021 21:36
Show Gist options
  • Save marcosgz/46088679de34bd72c5762423da901ffd to your computer and use it in GitHub Desktop.
Save marcosgz/46088679de34bd72c5762423da901ffd to your computer and use it in GitHub Desktop.
module AdapterOne
def instance_method
':one instance method'
end
alias one instance_method
end
module AdapterTwo
def instance_method
':two instance method'
end
alias two instance_method
end
class Foo
def initialize(adapter)
case adapter
when :one
initialize_one
when :two
initialize_two
end
end
attr_reader :adapter
def initialize_one
@adapter = :one
extend AdapterOne
end
def initialize_two
@adapter = :two
extend AdapterTwo
end
end
> foo = Foo.new(:nope)
=> #<Foo:0x00007f9703905910>
> foo.respond_to?(:one)
=> false
> foo.respond_to?(:instance_method)
=> false
> foo1 = Foo.new(:one)
=> #<Foo:0x00007f970302b8a8 @adapter=:one>
> foo1.respond_to?(:one)
=> true
> foo1.respond_to?(:instance_method)
=> true
> foo1.instance_method
=> ":one instance method"
> foo2 = Foo.new(:two)
=> #<Foo:0x00007f9703824a78 @adapter=:two>
> foo2.respond_to?(:two)
=> true
> foo2.respond_to?(:instance_method)
=> true
> foo2.instance_method
=> ":two instance method"
foo = Foo.new(:nope)
foo.respond_to?(:one)
foo.respond_to?(:instance_method)
foo1 = Foo.new(:one)
foo1.respond_to?(:one)
foo1.respond_to?(:instance_method)
foo1.instance_method
foo2 = Foo.new(:two)
foo2.respond_to?(:two)
foo2.respond_to?(:instance_method)
foo2.instance_method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment