Last active
September 4, 2017 15:06
-
-
Save kateinoigakukun/867b485c82dc1dc858d933496298fd25 to your computer and use it in GitHub Desktop.
Ruby keyword arguments extention
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
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