簡體   English   中英

Ruby - 關鍵字參數 - 你能把所有的關鍵字參數都當作一個散列嗎? 如何?

[英]Ruby - Keyword Arguments - Can you treat all of the keyword arguments as a hash? How?

我有一個看起來像這樣的方法:

def method(:name => nil, :color => nil, shoe_size => nil) 
  SomeOtherObject.some_other_method(THE HASH THAT THOSE KEYWORD ARGUMENTS WOULD MAKE)
end

對於任何給定的調用,我可以接受可選值的任意組合。 我喜歡命名參數,因為我可以只查看方法的簽名以查看可用的選項。

我不知道我在上面的代碼示例中用大寫字母描述的內容是否有快捷方式。

在過去,它曾經是:

def method(opts)
  SomeOtherObject.some_other_method(opts)
end

優雅,簡單,幾乎是作弊。

這些關鍵字參數是否有快捷方式,或者我是否必須在方法調用中重新構建我的選項哈希?

是的,這可能的,但它不是很優雅。

您必須使用parameters方法,它返回方法參數及其類型的數組(在這種情況下,我們只有關鍵字參數)。

def foo(one: 1, two: 2, three: 3)
  method(__method__).parameters
end  
#=> [[:key, :one], [:key, :two], [:key, :three]]

知道了這一點,有多種方法可以使用該數組來獲取所有參數及其提供的值的哈希值。

def foo(one: 1, two: 2, three: 3)
  params = method(__method__).parameters.map(&:last)
  opts = params.map { |p| [p, eval(p.to_s)] }.to_h
end
#=> {:one=>1, :two=>2, :three=>3}

所以你的例子看起來像

def method(name: nil, color: nil, shoe_size: nil)
  opts = method(__method__).parameters.map(&:last).map { |p| [p, eval(p.to_s)] }.to_h
  SomeOtherObject.some_other_method(opts)
end

仔細考慮使用這個。 它很聰明,但以可讀性為代價,其他閱讀您代碼的人不會喜歡它。

您可以使用輔助方法使其更具可讀性。

def params # Returns the parameters of the caller method.
  caller_method = caller_locations(length=1).first.label  
  method(caller_method).parameters 
end

def method(name: nil, color: nil, shoe_size: nil)
  opts = params.map { |p| [p, eval(p.to_s)] }.to_h
  SomeOtherObject.some_other_method(opts)
end

更新: Ruby 2.2 引入了Binding#local_variables ,可以用來代替Method#parameters 請注意,因為您必須在方法內定義任何其他局部變量之前調用local_variables

# Using Method#parameters
def foo(one: 1, two: 2, three: 3)
  params = method(__method__).parameters.map(&:last)
  opts = params.map { |p| [p, eval(p.to_s)] }.to_h
end
#=> {:one=>1, :two=>2, :three=>3}

# Using Binding#local_variables (Ruby 2.2+)
def bar(one: 1, two: 2, three: 3)
  binding.local_variables.params.map { |p|
    [p, binding.local_variable_get(p)]
  }.to_h
end
#=> {:one=>1, :two=>2, :three=>3}

當然! 只需使用雙 splat ( ** ) 運算符。

def print_all(**keyword_arguments)
  puts keyword_arguments
end

def mixed_signature(some: 'option', **rest)
  puts some
  puts rest
end

print_all example: 'double splat (**)', arbitrary: 'keyword arguments'
# {:example=>"double splat (**)", :arbitrary=>"keyword arguments"}

mixed_signature another: 'option'
# option
# {:another=>"option"}

它的工作方式與常規 splat ( * ) 一樣,用於收集參數。 您甚至可以將關鍵字參數轉發給另一個方法。

def forward_all(*arguments, **keyword_arguments, &block)
  SomeOtherObject.some_other_method *arguments,
                                    **keyword_arguments,
                                    &block
end

我玩得很開心,所以謝謝你。 這是我想出的:

describe "Argument Extraction Experiment" do
  let(:experiment_class) do
    Class.new do
      def method_with_mixed_args(one, two = 2, three:, four: 4)
        extract_args(binding)
      end

      def method_with_named_args(one:, two: 2, three: 3)
        extract_named_args(binding)
      end

      def method_with_unnamed_args(one, two = 2, three = 3)
        extract_unnamed_args(binding)
      end

      private

      def extract_args(env, depth = 1)
        caller_param_names = method(caller_locations(depth).first.label).parameters
        caller_param_names.map do |(arg_type,arg_name)|
          { name: arg_name, value: eval(arg_name.to_s, env), type: arg_type }
        end
      end

      def extract_named_args(env)
        extract_args(env, 2).select {|arg| [:key, :keyreq].include?(arg[:type]) }
      end

      def extract_unnamed_args(env)
        extract_args(env, 2).select {|arg| [:opt, :req].include?(arg[:type]) }
      end
    end
  end

  describe "#method_with_mixed_args" do
    subject { experiment_class.new.method_with_mixed_args("uno", three: 3) }
    it "should return a list of the args with values and types" do
      expect(subject).to eq([
        { name: :one,    value: "uno", type: :req },
        { name: :two,    value: 2,     type: :opt },
        { name: :three,  value: 3,     type: :keyreq },
        { name: :four,   value: 4,     type: :key }
      ])
    end
  end

  describe "#method_with_named_args" do
    subject { experiment_class.new.method_with_named_args(one: "one", two: 4) }
    it "should return a list of the args with values and types" do
      expect(subject).to eq([
        { name: :one,    value: "one", type: :keyreq },
        { name: :two,    value: 4,     type: :key },
        { name: :three,  value: 3,     type: :key }
      ])
    end
  end

  describe "#method_with_unnamed_args" do
    subject { experiment_class.new.method_with_unnamed_args(2, 4, 6) }
    it "should return a list of the args with values and types" do
      expect(subject).to eq([
        { name: :one,    value: 2,  type: :req },
        { name: :two,    value: 4,  type: :opt },
        { name: :three,  value: 6,  type: :opt }
      ])
    end
  end
end

我選擇返回一個數組,但您可以輕松地修改它以返回一個散列(例如,在初始檢測后不關心參數類型)。

下面的語法怎么樣?

要使其工作,請將params視為方法中的保留關鍵字,並將此行放在方法的頂部。

def method(:name => nil, :color => nil, shoe_size => nil) 
  params = params(binding)

  # params now contains the hash you're looking for
end

class Object
  def params(parent_binding)
    params = parent_binding.local_variables.reject { |s| s.to_s.start_with?('_') || s == :params }.map(&:to_sym)

    return params.map { |p| [ p, parent_binding.local_variable_get(p) ] }.to_h
  end
end

@Dennis 的回答很有用,也很有教育意義。 但是,我注意到Binding#local_variables將返回所有局部變量,無論local_variables何時執行:

def locals_from_binding(binding_:)
  binding_.local_variables.map { |var|
    [var, binding_.local_variable_get(var)]
  }.to_h
end

def m(a:, b:, c:)
  args = locals_from_binding(binding_: binding)
  pp args

  d = 4
end

m(a: 1, b: 3, c: 5)
# Prints:
#   {:a=>1, :b=>3, :c=>5, :args=>nil, :d=>nil}
# Note the presence of :d

我提出了一個混合解決方案:

def method_args_from_parameters(binding_:)
  method(caller_locations[0].label)
  .parameters.map(&:last)
  .map { |var|
    [var, binding_.local_variable_get(var)]
  }.to_h
end

def m(a:, b:, c:)
  args = method_args_from_parameters(binding_: binding)
  pp args

  d = 4
end

m(a: 1, b: 3, c: 5)
# Prints:
#   {:a=>1, :b=>3, :c=>5}
# Note the absence of :d

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM