[英]How do methods use hash arguments in Ruby?
I saw hash arguments used in some library methods as I've been learning. 我一直在学习,在某些库方法中看到了哈希参数。
Eg, 例如,
list.search(:titles, genre: 'jazz', duration_less_than: 270)
Can someone explain how a method uses arguments like this, and how you could create a method that makes use of Hash arguments? 有人可以解释一个方法如何使用这样的参数,以及如何创建一个使用哈希参数的方法吗?
Example: 例:
def foo(regular, hash={})
puts "regular: #{regular}"
puts "hash: #{hash}"
puts "a: #{hash[:a]}"
puts "b: #{hash[:b]}"
end
foo("regular argument", a: 12, :b => 13)
I use hash={}
to specify that the last argument is a hash, with default value of empty hash. 我使用
hash={}
来指定最后一个参数是哈希,默认值为空哈希。 Now, when I write: 现在,当我写:
foo("regular argument", a: 12, :b => 13)
It's actually a syntactic sugar for: 它实际上是以下方面的语法糖:
foo("regular argument", {a: 12, :b => 13})
Also, {a: 12}
is syntactic sugar for {:a => 12}
. 同样,
{a: 12}
是{:a => 12}
语法糖。
When all of this is combined together, you get a syntax that looks similar to named arguments in other languages. 将所有这些组合在一起后,您将获得一种语法,该语法看起来与其他语言中的命名参数相似。
In Ruby 2.x, you can use **
hash splat: 在Ruby 2.x中,您可以使用
**
hash splat:
def foo( ordered_argument, **named_arguments )
puts "Ordered argument: #{ordered_argument}"
puts "Named arguments: #{named_arguments}"
end
foo( :titles, genre: 'jazz', duration_less_than: 270 )
#=> Ordered argument: titles
#=> Named arguments: {:genre=>"jazz", :duration_less_than=>270}
When a Ruby method call's argument list ends in one or more key-value pairs, like foo: 'bar'
or 'foo' => 1
, Ruby collects them all into a single hash and passes that hash as the last parameter. 当Ruby方法调用的参数列表以一个或多个键值对结尾(例如
foo: 'bar'
或'foo' => 1
,Ruby会将它们全部收集到单个哈希中,并将该哈希作为最后一个参数传递。 You can see that yourself in irb
: 您可以在
irb
看到自己:
irb(main):002:0> puts foo: 'bar', baz: 'quux'
{:foo=>"bar", :baz=>"quux"}
=> nil
Thus, you can add a final, optional parameter to a method you're writing to receive this hash. 因此,您可以将最终的可选参数添加到正在编写的用于接收此哈希的方法中。 You'll usually want to default it to an empty hash.
通常,您通常需要将其默认为空哈希。 You can call the parameter anything you want, but
options
is a common name: 您可以根据需要调用参数,但是
options
是一个通用名称:
def my_method(a, b, c, options = {})
...
end
One useful trick if you're using Rails: It's often handy to treat plain strings and symbols as equivalent. 如果使用Rails,一个有用的技巧是:将纯字符串和符号视为等效通常很方便。 Rails adds a
symbolize_keys!
Rails添加了
symbolize_keys!
method to Hash
to convert all string keys to symbols: Hash
方法,将所有字符串键转换为符号:
def my_method(a, b, c, options = {})
options.symbolize_keys!
...
end
I would do one of two options: 我会选择以下两种方法之一:
1- if a got a large number of arguments to pass into a method I would use a hash like this: 1-如果有大量参数要传递给方法,我将使用如下哈希:
some_method({titles => 'titulo', genre => 'jazz', duration_less_than => 270})
or 要么
my_hash = {titles => 'titulo', genre => 'jazz', duration_less_than => 270}
some_method(my_hash)
and 和
def some_method(hash_options)
#important code
end
2- option will be more 'traditional' 2-选项将更“传统”
some_method('titulo', 'jazz', 270)
def some_method(titles, genre, duration_less_than)
#important code
end
Since Ruby 2.0 you can use keyword arguments [1][2] as opposed to the single Hash parameter. 从Ruby 2.0开始,您可以使用关键字参数[1] [2]来代替单个Hash参数。
def foo(keyword_arg: 'bar')
keyword_arg
end
And here's how it behaves. 这是它的行为方式。
> foo
=> "bar"
> foo(keyword_arg: 'baz')
=> "baz"
This is how I do it: 这是我的方法:
def my_method(title, args)
puts title
puts args
passing parameters: 传递参数:
my_method('test title', a: 'foo', b: 'bar')
# => test title
# => '{:a => 'foo', :b => 'bar'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.