Skip to content

Instantly share code, notes, and snippets.

@msavy
Forked from chriseppstein/module_inclusion.rb
Created January 12, 2012 02:14
Show Gist options
  • Save msavy/1598128 to your computer and use it in GitHub Desktop.
Save msavy/1598128 to your computer and use it in GitHub Desktop.
module Wrapped
# This is the wrapper class
class Wrapper
attr_accessor :content
def initialize(content)
@content = content
end
def content_value
@content.value
end
end
# So that module can be inluded as well as extended
def self.included(base)
base.extend self
end
def new(*args)
Wrapper.new
super
end
end
class Content
include Wrapped
attr_accessor :value
def initialize(default)
@value = default
end
end
class ComplicatedContent < Content
alias_method :simple_value, :value
def value
simple_value.reverse
end
end
c = Content.new("a_to_z")
puts "Content.new(\"a_to_z\"):"
puts "- class = #{c.class}"
puts "- value = #{c.content_value}"
c = ComplicatedContent.new("a_to_z")
puts "ComplicatedContent.new(\"a_to_z\"):"
puts "- class = #{c.class}"
puts "- value = #{c.content_value}"
class Wrapper
attr_accessor :content
def initialize(content)
@content = content
end
def content_value
@content.value
end
end
class Content
attr_accessor :value
class << self
def new(*args)
Wrapper.new(super)
end
end
def initialize(default)
@value = default
end
end
class ComplicatedContent < Content
alias_method :simple_value, :value
def value
simple_value.reverse
end
end
c = Content.new("a_to_z")
puts "Content.new(\"a_to_z\"):"
puts "- class = #{c.class}"
puts "- value = #{c.content_value}"
c = ComplicatedContent.new("a_to_z")
puts "ComplicatedContent.new(\"a_to_z\"):"
puts "- class = #{c.class}"
puts "- value = #{c.content_value}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment