Skip to content

Instantly share code, notes, and snippets.

@Fryie
Created February 1, 2016 13:27
Show Gist options
  • Save Fryie/667791e574a1ae3cb47d to your computer and use it in GitHub Desktop.
Save Fryie/667791e574a1ae3cb47d to your computer and use it in GitHub Desktop.
easy keyword classes
module KClass
def self.[](*keywords)
klass = Class.new
klass.send(:define_method, :initialize) do |hash|
keywords.each do |kw|
instance_variable_set("@#{kw}", hash[kw])
end
end
keywords.each do |kw|
klass.send :attr_reader, kw
end
klass
end
end
PaymentProcessor = KClass[:order, :user]
class PaymentProcessor
def put_user
puts user
end
end
PaymentProcessor.new(order: 'foo', user: 'bar').put_user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment