Skip to content

Instantly share code, notes, and snippets.

@pigoz
Created February 6, 2010 19:22
Show Gist options
  • Save pigoz/296910 to your computer and use it in GitHub Desktop.
Save pigoz/296910 to your computer and use it in GitHub Desktop.
module Kernel
def meta
class << self; self; end
end
end
class Module
public :attr_accessor, :attr_reader, :attr_writer
end
class Spaceship
def self.make(attributes={}, &block)
return_value = self.new
return_value.instance_eval(&block)
return_value
end
def self.can_have(variable)
self.attr_accessor variable
end
def method_missing(id, *args, &block)
case(id.to_s)
when /^add_(.*)/
return unless self.respond_to?("#{$1}s")
var_name = "@#{$1}s"
instance_variable_set(var_name, []) if instance_variable_get(var_name).nil?
instance_variable_get(var_name) << args[0]
else
super
end
end
end
class AlienSpaceship < Spaceship
can_have :engines
can_have :weapons
end
s = AlienSpaceship.make do
add_engine "Anti-matter NGIN 1"
add_engine "Anti-matter NGIN 2"
add_engine "Interdimensional Warp Drive"
add_weapon "Positron Cannons YKBZ"
end
puts s.engines
puts s.weapons
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment