简体   繁体   English

Ruby中表达式右侧的逗号字符是什么

[英]What is the comma character meaning in right side of expression in Ruby

Excuse me for this basic question but, I couldn't find the answer on Google. 请问这个基本问题,但我在Google上找不到答案。 I am new to Ruby and came across to this line of code: 我是Ruby的新手,遇到过以下代码行:

self.primary_keys = :role_id, :action_name

What I understand from it is that self.primary_keys is a Class variable and is assigned an array or hash of symbols? 我从中了解到self.primary_keys是一个Class变量,并被分配了一个数组或符号哈希? What means the right side of the expression ( :role_id, :action_name )? 表达式( :role_id, :action_name )的右边是什么意思? What is the type of it? 它是什么类型?

It's two symbols separated by a comma, and is an implicit array. 它是两个用逗号隔开的符号,是一个隐式数组。

Equivalent to 相当于

self.primary_keys = [:role_id, :action_name]

It's more common to see the technique used on the left side of an assignment. 看到作业左侧使用的技术更为常见。

name, age = ["George", 21]

puts name
=> "George"

puts age
=> 21

The feature lets you swap the contents of variables without an intermediate variable. 该功能使您可以交换变量的内容而无需中间变量。

For example, in some languages to swap a and b you need a temporary variable 例如,在某些语言中,要交换ab您需要一个临时变量

temporary = a
a = b
b = temporary

In Ruby you can do 在Ruby中,您可以

a, b = b, a

It's assignment 这是作业

x, y = ["Srini", 25] x,y = [“ Srini”,25]

puts x => "Srini" 放x =>“ Srini”

puts y => 25 y => 25

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM