Skip to content

Instantly share code, notes, and snippets.

@kateinoigakukun
Last active September 4, 2017 15:06
Show Gist options
  • Save kateinoigakukun/867b485c82dc1dc858d933496298fd25 to your computer and use it in GitHub Desktop.
Save kateinoigakukun/867b485c82dc1dc858d933496298fd25 to your computer and use it in GitHub Desktop.
Ruby keyword arguments extention
class BasicObject
class << self
def func(args, &block)
method = args[:method]
args_map = args[:args]
define_method(method) do |args|
keys = args_map.values.map { |v| ":#{v}" }.join(',')
values = args_map.keys.map { |k| "'#{args[k]}'"}.join(',')
s = eval("Struct.new(#{keys}).new(#{values})")
s.instance_eval(&block)
end
end
def method_missing(method, *args)
super unless args.first.is_a?(Hash)
return {:args => args.first, :method => method}
end
end
end
class A
func write(with: :text, for: :pipe) do
puts text
end
end
a = A.new
a.write(with: "message", for: "hoge")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment