[英]Clarification on Ruby syntax in Rails
I'm learning Rails and in going through the official guides , I came across some code which I could not really understand the meaning of. 我正在学习Rails,并在阅读官方指南时遇到了一些我无法真正理解其含义的代码。
Case 1 - 情况1 -
class Person < ApplicationRecord
validates :name, presence: true
end
It looks to me that validates
is a method that takes a symbol called :name
as an argument. 在我看来,
validates
是一种使用称为:name
的符号作为参数的方法。 But then, what is presence
? 但是,什么是
presence
? Is it also a method? 这也是一种方法吗? But if it is, what is the significance of the
:
right after presence
. 但如果是,是什么的意义
:
之后presence
。 I understand that the value true
is being set for presence
, which serves as kind of a validation, requiring the presence of (in other words). 据我所知,值
true
正被设置为presence
,其用作一种验证的,需要的存在(换言之)。 But I'm not quite clear on the syntax. 但是我在语法上不太清楚。
It might also be possible that presence: true
is just a hash, where :presence
(the symbol) is the key, and true
is the value. presence: true
可能只是一个哈希,其中:presence
(符号)是键,而true
是值。
Case 2 - 情况2-
class Person < ApplicationRecord
validates :terms_of_service, acceptance: true, message: 'must be abided'
end
Again, validates
is the method that takes a symbol :terms_of_service
as an argument. 同样,
validates
是一个以符号:terms_of_service
作为参数的方法。 But what about the rest? 但是其余的呢? Is it a hash with 2 key-value pairs, somewhat like
{acceptance: true, message: 'must be abided'}
? 它是具有2个键值对的哈希,有点像
{acceptance: true, message: 'must be abided'}
吗?
And if it is indeed a hash, why is it tacked on to the validates
method in each case? 如果确实是哈希,为什么还要在每种情况下都将其附加到
validates
方法? Why can't it be 为什么不能
validates :terms_of_service
acceptance: true, message: 'must be abided'
Thanks for the help! 谢谢您的帮助!
That is the syntax for passing a hash to the method. 这是将哈希传递给方法的语法。 What that is doing is the same thing as
validates(:terms_of_service, {acceptance: true, message: 'must be abided'})
. 所做的事情与
validates(:terms_of_service, {acceptance: true, message: 'must be abided'})
。 It's a common way of passing extra options to a method. 这是将额外选项传递给方法的常用方法。
In Ruby there's a strong tradition for passing in options as a Hash as the last argument, strong enough that this tradition became new feature borrowed from Python: Keyword arguments. 在Ruby中,有一个很强的传统,就是将选项作为哈希传递给最后一个参数,这种强大的传统使它成为从Python借来的新功能:关键字参数。
In classic Ruby the method would be defined as this: 在经典的Ruby中,方法将定义为:
def validates(*args)
options = args.last.is_a?(Hash) ? args.pop : { }
# args is a list of fields
end
In Ruby 2.3 you can do this: 在Ruby 2.3中,您可以执行以下操作:
def validates(*args, **options)
# options is automatically any hash-style arguments if present
end
In Ruby 2.0+ you can also do this: 在Ruby 2.0+中,您还可以执行以下操作:
def validates(*args, acceptance: false, message: nil)
end
Where that defines options as first-class variables. 其中将选项定义为一流变量。
It's a common Ruby pattern, so it's good to understand what's going on here. 这是一种常见的Ruby模式,因此最好了解这里发生的情况。 Try writing your own methods that take options and you'll see how it plays out.
尝试编写自己的带有选项的方法,您将了解其效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.