简体   繁体   中英

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. I am new to Ruby and came across to this line of code:

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? What means the right side of the expression ( :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

temporary = a
a = b
b = temporary

In Ruby you can do

a, b = b, a

It's assignment

x, y = ["Srini", 25]

puts x => "Srini"

puts y => 25

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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