Skip to content

Instantly share code, notes, and snippets.

@bswinnerton
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save bswinnerton/7a73bcfc442c7be23525 to your computer and use it in GitHub Desktop.

Select an option

Save bswinnerton/7a73bcfc442c7be23525 to your computer and use it in GitHub Desktop.
require 'pry'
$v2 = true
module V2
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def v2_feature
yield if $v2
end
end
end
class Foo
include V2
def bar
"I'm a V1 feature"
end
v2_feature do
def baz
"I'm a V2 feature"
end
def call_private_baz
private_baz
end
private
def private_baz
"I'm a private V2 feature"
end
end
end
foo = Foo.new
foo.bar
#=> "I'm a V1 feature"
foo.baz
#=> "I'm a V2 feature"
foo.private_baz
#=> NoMethodError: private method `private_baz' called for #<Foo:0x007ff3029f2ea8>
foo.call_private_baz
#=> I'm a hidden V2 feature
$v2 = false # The file must be reloaded for this to take effect in the V2 module
foo = Foo.new
foo.bar
#=> "I'm a V1 feature"
foo.baz
#=> NoMethodError: undefined method `baz' for #<Foo:0x007fb824105728>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment