Created
August 21, 2009 19:56
-
-
Save lsdr/172388 to your computer and use it in GitHub Desktop.
Experimentations with Ruby's super
This file contains hidden or 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 Upstream | |
module Streamer | |
def self.included(receiver) | |
receiver.extend ExtensionMethods | |
end | |
module ExtensionMethods | |
def streamable! | |
self.send :include, InstanceMethods | |
end | |
end | |
module InstanceMethods | |
def index! | |
execute_on_index('index!') | |
end | |
protected | |
def execute_on_index(text) | |
"Streamer #{text.upcase}!" | |
end | |
def insert_on_index | |
execute_on_index('insert on index') | |
end | |
end | |
end | |
end | |
class Scopable | |
include Upstream::Streamer | |
streamable! | |
attr_accessor :scope | |
def index! | |
unless scope.nil? | |
execute_on_index('uh knoh') | |
else | |
super | |
end | |
end | |
protected | |
def insert_on_index | |
unless scope.nil? | |
execute_on_index('insert on uh knoh!') | |
else | |
super | |
end | |
end | |
end | |
s = Scopable.new | |
puts s.index! | |
puts s.send :insert_on_index | |
s.scope = :local | |
puts s.index! | |
puts s.send :insert_on_index |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment