Skip to content

Instantly share code, notes, and snippets.

@chriseppstein
Created February 12, 2009 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save chriseppstein/62556 to your computer and use it in GitHub Desktop.
Save chriseppstein/62556 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