Skip to content

Instantly share code, notes, and snippets.

@chikamichi
Created March 21, 2010 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chikamichi/339025 to your computer and use it in GitHub Desktop.
Save chikamichi/339025 to your computer and use it in GitHub Desktop.
plugins system: standalone
module Base
class Server
def blabla(argument)
@config ||= "default"
if @config == "default"
p "Server says: #{argument}"
end
puts "config: #{@config}"
end
end
end
module Base
module Plugins
module Something
def blabla(argument)
@config ||= "Something"
if @config == "Something"
p "Server (Somethinged) says: #{argument}"
end
super
end
end
end
end
g = Base::Server.new
puts g.instance_eval("class << self; self; end").ancestors.inspect
puts
g.blabla("hello world")
puts
puts "---"
puts
g.extend(Base::Plugins::Something)
puts g.instance_eval("class << self; self; end").ancestors.inspect
puts
g.blabla("hello world from Something")
puts
puts "---"
puts
module Base
module Plugins
module SomethingElse
def blabla(*arguments)
@config = "SomethingElse"
puts arguments.join " "
super arguments.join " "
end
end
end
end
g.extend(Base::Plugins::SomethingElse)
puts g.instance_eval("class << self; self; end").ancestors.inspect
puts
g.blabla("Hello", "World", "From", "Something", "Else")
puts
# =>
# [Base::Server, Object, Kernel]
# "Server says: hello world"
# config: default
# ---
# [Base::Plugins::Something, Base::Server, Object, Kernel]
# "Server says: hello world from Something"
# config: default
# ---
# [Base::Plugins::SomethingElse, Base::Plugins::Something, Base::Server, Object, Kernel]
# Hello World From Something Else
# config: SomethingElse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment