简体   繁体   中英

Ruby: grammar name for :variable

I'm learning rails by working with some examples. Here is my sample model file:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

The ruby grammar I don't know is:

 devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable

After some researchs, I know above command uses poetry mode of ruby, it means method calling without parentheses. so above command should be:

devise(:database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable)

I still don't understand what is grammar name of :variable . I have searched but only know @variable for instance, @@variable for class ...

Thanks

Any word that is preceded by a : is called a symbol . As @Holger mentioned in his comment, a symbol is (at its simplest form) an immutable string.

Symbols are most commonly used as keys and to reference keys within a Hash :

hash = { key: "value" }

"value" can then be referenced by calling the hash's key as a symbol:

hash[:key]
=> "value"

These are called symbols. more here

http://ruby-doc.org/core-2.2.0/Symbol.html

I'm not strictly certain that this is what you are looking for, but any string of characters preceded by a : is a Symbol .

To summarize, a symbol is like a string, except each mention of the same symbol literal is a reference to the same immutable underlying value. In this case, each of those symbols ( :database_authenticable , :registerable , etc...) are giving specific values as arguments to the function devise , presumably setting certain attributes of it.

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