-
-
Save msavy/1598128 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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