Is there a way to pass a single argument to a ruby function that has only optional parameters?
有没有办法将单个参数传递给只有可选参数的ruby函数?
For instance, I want to pass ignoreAlreadyCrawled = true in the method below, and use the default parameter values for everything else. But since it's the last parameter, I have to do call crawl(nil, nil, nil, false, nil, false, nil, false, [], true), which is verbose, and ugly.
例如,我想在下面的方法中传递ignoreAlreadyCrawled = true,并对其他所有内容使用默认参数值。但由于它是最后一个参数,我必须调用crawl(nil,nil,nil,false,nil,false,nil,false,[],true),这是冗长的,丑陋的。
Method:
def crawl(url = nil, proxy = nil, userAgent = nil, post = false, postData = nil, image = false, imageFilename = nil, forceNoUserAgent = false, ignoreSubDomains = [], ignoreAlreadyCrawled = false)
#..logic here
end
2 个解决方案
#1
8
This is possible for ruby >= 2.0 with keyword arguments. In that case, you have to modify the method signature as follows (basically change =
to :
):
对于带有关键字参数的ruby> = 2.0,这是可能的。在这种情况下,您必须按如下方式修改方法签名(基本上是change = to :):
def crawl(url: nil, proxy: nil, userAgent: nil, post: false, postData: nil, image: false, imageFilename: nil, forceNoUserAgent: false, ignoreSubDomains: [], ignoreAlreadyCrawled: false)
#..logic here
end
And it will work as expected:
它将按预期工作:
crawl(ignoreAlreadyCrawled: true)
Please see article for more examples: https://robots.thoughtbot.com/ruby-2-keyword-arguments
有关更多示例,请参阅文章:https://robots.thoughtbot.com/ruby-2-keyword-arguments
#2
1
When you have more than two or three arguments, the preferred pattern is to use a Hash of options instead.
当您有两个或三个以上的参数时,首选模式是使用Hash of options。
I would redefine this method as follows: this is similar to Pawel's but more readable, i think:
我会按如下方式重新定义这个方法:这类似于Pawel,但更具可读性,我认为:
def crawl(options={})
defaults = { url: nil,
proxy: nil,
userAgent: nil,
post: false,
postData: nil,
image: false,
imageFilename: nil,
forceNoUserAgent: false,
ignoreSubDomains: [],
ignoreAlreadyCrawled: false
}
options = defaults.merge(options)
#..logic here
end
You could omit from defaults all options that have nil
as their default, as an undefined option will come out as nil anyway. But, having them there acts as documentation for which options can be passed.
您可以从默认值中省略所有将nil作为默认值的选项,因为未定义的选项无论如何都将以nil形式出现。但是,让他们在那里充当可以传递选项的文档。
#1
8
This is possible for ruby >= 2.0 with keyword arguments. In that case, you have to modify the method signature as follows (basically change =
to :
):
对于带有关键字参数的ruby> = 2.0,这是可能的。在这种情况下,您必须按如下方式修改方法签名(基本上是change = to :):
def crawl(url: nil, proxy: nil, userAgent: nil, post: false, postData: nil, image: false, imageFilename: nil, forceNoUserAgent: false, ignoreSubDomains: [], ignoreAlreadyCrawled: false)
#..logic here
end
And it will work as expected:
它将按预期工作:
crawl(ignoreAlreadyCrawled: true)
Please see article for more examples: https://robots.thoughtbot.com/ruby-2-keyword-arguments
有关更多示例,请参阅文章:https://robots.thoughtbot.com/ruby-2-keyword-arguments
#2
1
When you have more than two or three arguments, the preferred pattern is to use a Hash of options instead.
当您有两个或三个以上的参数时,首选模式是使用Hash of options。
I would redefine this method as follows: this is similar to Pawel's but more readable, i think:
我会按如下方式重新定义这个方法:这类似于Pawel,但更具可读性,我认为:
def crawl(options={})
defaults = { url: nil,
proxy: nil,
userAgent: nil,
post: false,
postData: nil,
image: false,
imageFilename: nil,
forceNoUserAgent: false,
ignoreSubDomains: [],
ignoreAlreadyCrawled: false
}
options = defaults.merge(options)
#..logic here
end
You could omit from defaults all options that have nil
as their default, as an undefined option will come out as nil anyway. But, having them there acts as documentation for which options can be passed.
您可以从默认值中省略所有将nil作为默认值的选项,因为未定义的选项无论如何都将以nil形式出现。但是,让他们在那里充当可以传递选项的文档。