[英]Ruby questions on def
I am very new to ruby and I would like to understand few syntax.我对 ruby 很陌生,我想了解一些语法。 When I define something like,
当我定义类似的东西时,
def attribute
{
id: @object.id,
password: @object.password
non_academic: !!@object.non_academic
}.merge(subject)
end
what does the codes inside "{}" do, and what are they call. “{}”中的代码有什么作用,它们叫什么。 Also, what does line like
id: @object.id,
means.另外,像
id: @object.id,
这样的行是什么意思。
It is a method with name attribute
( def attribute
) that returns a Hash
(curly braces ´{}´) with Symbol
s as keys ( id:
, password:
, ...).它是一个具有名称
attribute
( def attribute
)的方法,它返回一个Hash
(花括号“{}”),以Symbol
s 作为键( id:
, password:
, ...)。 The values are taken from attributes of an instance variable ( @object
) Then the Hash is also combined with another one ( merge
)这些值取自实例变量的属性(
@object
)然后哈希也与另一个组合( merge
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.