Method: HTTPClient::Util#keyword_argument

Defined in:
lib/httpclient/util.rb

#keyword_argument(args, *field) ⇒ Object

Keyword argument helper.

args

given arguments.

*field

a list of arguments to be extracted.

You can extract 3 arguments (a, b, c) with:

include Util
def my_method(*args)
a, b, c = keyword_argument(args, :a, :b, :c)
...
end
my_method(1, 2, 3)
my_method(:b => 2, :a = 1)

instead of;

def my_method(a, b, c)
...
end


93
94
95
96
97
98
99
100
101
# File 'lib/httpclient/util.rb', line 93

def keyword_argument(args, *field)
  if args.size == 1 and Hash === args[0]
    h = args[0]
    if field.any? { |f| h.key?(f) }
      return h.values_at(*field)
    end
  end
  args
end