gpt4 book ai didi

带有 optional 和 &block 参数的 Ruby 方法

转载 作者:数据小太阳 更新时间:2023-10-29 08:30:37 25 4
gpt4 key购买 nike

  1. 你好

    是否可以将可选属性和 block 作为参数用于方法调用?

    例子:我必须打电话

    method(foo, foo: bar, -> { do_something }

    尝试过

    def method(foo, *bar, &block)
    end

    至于我的理解, block 必须始终位于最后位置?

    经过一番研究,我发现一元 (?) * 似乎是为了阵列。由于我尝试传递一个 Hash 我将代码更改为

    def method(foo, bar={}, &block)
    end

    但这也不起作用。我猜是因为他不能找出栏结束和 block 开始的位置。

    有什么想法或建议吗?提前谢谢你

    附加:只是出于好奇我为什么需要这个。我们有一个大 jsonschema 运行并且有一个小的 DSL 从构建 json模型定义。无需详细介绍,我们希望实现 exportable_scopes。

    class FooBar
    exportable_scope :some_scope, title: 'Some Scope', -> { rewhere archived: true }
    end

    在某些初始化器上,这应该会发生:

    def exportable_scope scope, attributes, &block
    scope scope block
    if attributes.any?
    attributes.each do |attribute|
    exportable_schema.scopes[scope] = attribute
    end
    else
    exportable_schema.scopes[scope] = {title: scope}
    end
    end

    所以这工作正常,我只需要一个方法提示参数。

最佳答案

是的,这是可能的。

当混合不同种类的参数时,它们必须以特定顺序包含在方法定义中:

  1. 位置参数(必需和可选)和单个 splat 参数,顺序不限;
  2. 关键字参数(必需和可选),顺序不限;
  3. 双splat参数;
  4. block 参数(以&为前缀);

上面的顺序有些灵活。我们可以定义一个方法,并以单个 splat 参数开始参数列表,然后是几个可选的位置参数,等等。尽管 Ruby 允许这样做,但这通常是一种非常糟糕的做法,因为代码将难以阅读,甚至更难调试。通常最好使用以下顺序:

  1. 必需的位置参数;
  2. 可选的位置参数(具有默认值);
  3. 单个splat参数;
  4. 关键字参数(必填和可选,顺序无关);
  5. 双splat参数;
  6. 显式 block 参数(以 & 为前缀)。

例子:

def meditate cushion, meditation="kinhin", *room_items, time: , posture: "kekkafuza", **periods, &b
puts "We are practicing #{meditation}, for #{time} minutes, in the #{posture} posture (ouch, my knees!)."
puts "Room items: #{room_items}"
puts "Periods: #{periods}"
b.call # Run the proc received through the &b parameter
end

meditate("zafu", "zazen", "zabuton", "incense", time: 40, period1: "morning", period2: "afternoon" ) { puts "Hello from inside the block" }

# Output:
We are practicing zazen, for 40 minutes, in the kekkafuza posture (ouch, my knees!).
Room items: ["zabuton", "incense"]
Periods: {:period1=>"morning", :period2=>"afternoon"}
Hello from inside the block

请注意,在调用该方法时,我们有:

  • 提供缓冲强制位置参数;
  • 覆盖冥想可选位置参数的默认值;
  • 通过 *room_items 参数传递了几个额外的位置参数(zabuton 和 incense);
  • 提供时间强制关键字参数;
  • 省略了姿势可选关键字参数;
  • 通过 **periods 参数传递了几个额外的关键字参数(period1:“morning”,period2:“afternoon”);
  • 通过 &b 参数传递了 block { puts "Hello from inside the block"};

请注意上面的服务器示例仅用于说明混合不同类型参数的可能性。在实际代码中构建这样的方法将是一种不好的做法。如果一个方法需要那么多参数,最好将它拆分成更小的方法。如果绝对有必要将那么多数据传递给单个方法,我们可能应该创建一个类以更有条理的方式存储数据,然后将该类的实例作为单个参数传递给该方法。

关于带有 optional 和 &block 参数的 Ruby 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38614973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com