# 標識符
Ruby的標識符用于命名局部變量、實例變量、類變量、全局變量、常量、方法以及塊。標識符的命名有一套固有規則。
### 命名規則
- 必須以字母、數字或下劃線為開頭,長度只受硬件內存大小限制
- Ruby的保留字不得用作標識符的命名。
### Ruby中標識符的分類
-
變量
- 本地變量, 也叫局部變量,比如 「name」。
- 實例變量,以@為開頭的變量,為實例對象所用,比如「@name」。
- 類實例變量, 也是以@為開頭的變量,為類所用,比如「@name」。
- 類變量, 以@@為開頭的變量,也是為類所用,比如「@@name」。
- 全局變量,全局范圍內都可以被使用的變量,類似于類變量,但是寫法不同,比如「$name」
- 常量, 以大寫字母為開頭,比如「Klass,Node」。Ruby中常量的值是可以被修改的,只是出一個警告。
-
方法與塊
方法和塊的命名規則,和本地變量一樣,如果是兩個單詞以上,用下劃線分割。比如「get_name」
### Chef中變量的命名
貼一段Chef的源碼,大家來感受一下:
~~~
class Chef
class Node
extend Forwardable
def_delegators :attributes, :keys, :each_key, :each_value, :key?, :has_key?
attr_accessor :recipe_list, :run_state, :override_runlist
# RunContext will set itself as run_context via this setter when
# initialized. This is needed so DSL::IncludeAttribute (in particular,
# #include_recipe) can access the run_context to determine if an attributes
# file has been seen yet.
#--
# TODO: This is a pretty ugly way to solve that problem.
attr_accessor :run_context
include Chef::Mixin::FromFile
include Chef::DSL::IncludeAttribute
include Chef::DSL::PlatformIntrospection
include Chef::Mixin::ParamsValidate
# Create a new Chef::Node object.
def initialize
@name = nil
@chef_environment = '_default'
@primary_runlist = Chef::RunList.new
@override_runlist = Chef::RunList.new
@attributes = Chef::Node::Attribute.new({}, {}, {}, {})
@run_state = {}
end
# ...
end
~~~
- 序
- Chapter 1: 初識Chef
- 一些背景
- Chef vs Puppet
- Chapter 2: Chef應用
- Chef架構
- Chef能做什么
- Chef組件
- Chef環境安裝
- chef-server
- opscode-chef
- chef-solo
- Chef實戰
- 實戰前的必修理論
- 使用Chef
- Chapter 3: Ruby基礎
- 對象與方法
- 標識符
- 類與模塊
- 數據類型
- 真與假
- 控制語句
- 代碼塊
- Chapter 4: Chef源碼架構
- Rubygems與gem
- bundler
- Chef源碼組織
- Chapter 5: Rails基礎
- Rails是什么
- MVC架構
- Restful
- Rails組成與項目結構
- Chapter 6: Chef Server WebUI
- Chef Server Webui組織結構
- Chef Rest API
- 參考