Ruby支持一系列豐富的運(yùn)算符的一個(gè)現(xiàn)代化的語(yǔ)言。大多數(shù)運(yùn)算符實(shí)際上是方法調(diào)用。例如,a + b的被解釋為a,
+(b)變量引用的對(duì)象的方法被稱(chēng)為一個(gè)用b作為它的參數(shù)。
對(duì)于每個(gè)運(yùn)算符 (+ - * / % ** | ^ >> ||), 有相應(yīng)的賦值運(yùn)算符縮寫(xiě)形式 (+= -= 等)
Ruby算術(shù)運(yùn)算符:
假設(shè)變量a=10,變量b=20:
![](http://img.jbzj.com/file_images/article/201505/201551292317953.jpg?201541292345)
Ruby比較操作符:
假設(shè)變量a=10,變量b=20:
![](http://img.jbzj.com/file_images/article/201505/201551292359303.png?201541292414)
Ruby賦值運(yùn)算符:
假設(shè)變量a=10,變量b=20:
![](http://img.jbzj.com/file_images/article/201505/201551292426433.png?201541292440)
Ruby并行賦值:
Ruby還支持并行賦值的變量。這使得多個(gè)一行Ruby代碼來(lái)初始化變量。例如:
需要更迅速聲明,使用并行賦值:
并行賦值交換兩個(gè)變量的值也是有用的:
Ruby位運(yùn)算符:
位運(yùn)算符位和位操作執(zhí)行位。
假設(shè)當(dāng)a =60和b=13;現(xiàn)在以二進(jìn)制格式將如下:
a = 0011 1100
b = 0000 1101
-----------------
ab = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
支持Ruby語(yǔ)言的位運(yùn)算符
![](http://img.jbzj.com/file_images/article/201505/201551292459659.jpg?201541292522)
Ruby邏輯運(yùn)算符:
支持Ruby語(yǔ)言的邏輯運(yùn)算符
假設(shè)變量a=10,變量b=20:
![](http://img.jbzj.com/file_images/article/201505/201551292600791.jpg?201541292624)
Ruby三元運(yùn)算符:
還有一個(gè)運(yùn)算符稱(chēng)為三元運(yùn)算符。這首先計(jì)算一個(gè)表達(dá)式為true或false值,然后執(zhí)行一個(gè)計(jì)算結(jié)果來(lái)決定兩個(gè)語(yǔ)句的哪一個(gè)。條件運(yùn)算符的語(yǔ)法如下:
![](http://img.jbzj.com/file_images/article/201505/201551292637298.jpg?201541292830)
Ruby范圍運(yùn)算符:
Ruby中的序列范圍是用來(lái)創(chuàng)建連續(xù)值 - 組成了開(kāi)始值/結(jié)束值,并在兩者之間的值的范圍內(nèi)。
在Ruby中,創(chuàng)建這些序列使用“..”和“...”范圍運(yùn)算符。這兩個(gè)點(diǎn)的形式建立一個(gè)包容性的范圍,而三個(gè)點(diǎn)的形式創(chuàng)建了一個(gè)范圍,不包括指定的最大值。
![](http://img.jbzj.com/file_images/article/201505/201551292848887.jpg?20154129295)
Ruby defined? 操作符:
defined是一個(gè)特殊的操作符采取的形式的方法調(diào)用,以確定是否通過(guò)表達(dá)式定義。如果沒(méi)有被定義的表達(dá)式,它返回一個(gè)描述字符串求解出的表達(dá)式或nil
有很多種用法 defined操作符:
用法 1
defined? variable # True if variable is initialized
例如 :
foo = 42
defined? foo # => "local-variable"
defined? $_ # => "global-variable"
defined? bar # => nil (undefined)
用法 2
defined? method_call # True if a method is defined
例如 :
defined? puts # => "method"
defined? puts(bar) # => nil (bar is not defined here)
defined? unpack # => nil (not defined here)
用法 3
# True if a method exists that can be called with super user
defined? super
例如 :
defined? super # => "super" (if it can be called)
defined? super # => nil (if it cannot be)
用法 4
defined? yield # True if a code block has been passed
例如 :
defined? yield # => "yield" (if there is a block passed)
defined? yield # => nil (if there is no block)
Ruby "." 雙冒號(hào) "::" 運(yùn)算符:
調(diào)用一個(gè)模塊方法,通過(guò)模塊的名稱(chēng)和句點(diǎn)它的名字前,引用一個(gè)常數(shù)使用該模塊的名稱(chēng)和兩個(gè)冒號(hào)。
::使得一元運(yùn)算符,常數(shù),實(shí)例方法和類(lèi)方法在類(lèi)或模塊定義,從任何地方訪(fǎng)問(wèn)外的類(lèi)或模塊。
請(qǐng)記?。涸赗uby中,類(lèi)和方法可以被視為常數(shù)。只需要前綴::Const_name的表達(dá)式返回相應(yīng)的類(lèi)或模塊對(duì)象。
如果沒(méi)有前綴表達(dá)式時(shí),主要對(duì)象類(lèi)默認(rèn)情況下使用。
這里有兩個(gè)例子:
MR_COUNT = 0 # constant defined on main Object class
module Foo
MR_COUNT = 0
::MR_COUNT = 1 # set global count to 1
MR_COUNT = 2 # set local count to 2
end
puts MR_COUNT # this is the global constant
puts Foo::MR_COUNT # this is the local "Foo" constant
Second Example:
CONST = ' out there'
class Inside_one
CONST = proc {' in there'}
def where_is_my_CONST
::CONST + ' inside one'
end
end
class Inside_two
CONST = ' inside two'
def where_is_my_CONST
CONST
end
end
puts Inside_one.new.where_is_my_CONST
puts Inside_two.new.where_is_my_CONST
puts Object::CONST + Inside_two::CONST
puts Inside_two::CONST + CONST
puts Inside_one::CONST
puts Inside_one::CONST.call + Inside_two::CONST
Ruby運(yùn)算符優(yōu)先級(jí)
下表列出了所有運(yùn)算符從最高優(yōu)先級(jí)到最低。
![](http://img.jbzj.com/file_images/article/201505/201551293021971.jpg?201541293135)
注: 方法列一個(gè)是運(yùn)算符實(shí)際上是方法,因此可能會(huì)被改寫(xiě)。
您可能感興趣的文章:- 使用Ruby on Rails和PostgreSQL自動(dòng)生成UUID的教程
- 簡(jiǎn)單介紹Ruby on Rails對(duì)PostgreSQL數(shù)組類(lèi)型的支持
- Ruby基礎(chǔ)知識(shí)之類(lèi)