Ruby-operators
Ruby-演算子
Rubyは現代の言語に期待されるように、豊富な演算子セットをサポートしています。 ほとんどの演算子は、実際にはメソッド呼び出しです。 たとえば、+ bはa。+(b)として解釈されます。ここで+変数_a_によって参照されるオブジェクト内のメソッドは、_b_を引数として呼び出されます。
各演算子(&plus;-/%*&| ^ << >> && ||)には、対応する形式の省略された代入演算子(&plus; =-=など)があります。
Rubyの算術演算子
変数aが10を保持し、変数bが20を保持すると仮定します-
Operator | Description | Example |
---|---|---|
PLUS | Addition − Adds values on either side of the operator. | a PLUS b will give 30 |
− | Subtraction − Subtracts right hand operand from left hand operand. | a - b will give -10 |
* | Multiplication − Multiplies values on either side of the operator. | a* b will give 200 |
/ | Division − Divides left hand operand by right hand operand. | b/a will give 2 |
% | Modulus − Divides left hand operand by right hand operand and returns remainder. | b % a will give 0 |
** | Exponent − Performs exponential (power) calculation on operators. | a**b will give 10 to the power 20 |
Ruby比較演算子
変数aが10を保持し、変数bが20を保持すると仮定します-
Operator | Description | Example |
---|---|---|
== | Checks if the value of two operands are equal or not, if yes then condition becomes true. | (a == b) is not true. |
!= | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. | (a != b) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (a > b) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (a < b) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (a >= b) is not true. |
⇐ | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (a ⇐ b) is true. |
<⇒ | Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second. | (a <⇒ b) returns -1. |
=== | Used to test equality within a when clause of a case statement. | (1…10) === 5 returns true. |
.eql? | True if the receiver and argument have both the same type and equal values. | 1 == 1.0 returns true, but 1.eql?(1.0) is false. |
equal? | True if the receiver and argument have the same object id. | if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true. |
Rubyの代入演算子
変数aが10を保持し、変数bが20を保持すると仮定します-
Operator | Description | Example |
---|---|---|
= | Simple assignment operator, assigns values from right side operands to left side operand. | c = a PLUS b will assign the value of a PLUS b into c |
PLUS= | Add AND assignment operator, adds right operand to the left operand and assign the result to left operand. | c PLUS= a is equivalent to c = c PLUS a |
-= | Subtract AND assignment operator, subtracts right operand from the left operand and assign the result to left operand. | c -= a is equivalent to c = c - a |
*= | Multiply AND assignment operator, multiplies right operand with the left operand and assign the result to left operand. | c *= a is equivalent to c = c * a |
/= | Divide AND assignment operator, divides left operand with the right operand and assign the result to left operand. | c/= a is equivalent to c = c/a |
%= | Modulus AND assignment operator, takes modulus using two operands and assign the result to left operand. | c %= a is equivalent to c = c % a |
**= | Exponent AND assignment operator, performs exponential (power) calculation on operators and assign value to the left operand. | c = a is equivalent to c = c a |
Rubyの並列割り当て
Rubyは、変数の並列割り当てもサポートしています。 これにより、Rubyコードの1行で複数の変数を初期化できます。 たとえば-
a = 10
b = 20
c = 30
これは、並列割り当てを使用してより迅速に宣言することができます-
a, b, c = 10, 20, 30
並列割り当てはまた、2つの変数に保持されている値を交換するのに便利です-
a, b = b, c
Rubyのビット演算子
ビット単位演算子はビットに対して機能し、ビットごとの演算を実行します。
a = 60の場合;およびb = 13;今バイナリ形式では、次のようになります-
a = 0011 1100
b = 0000 1101
------------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
次のビット演算子は、Ruby言語でサポートされています。
Operator | Description | Example |
---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (a & b) will give 12, which is 0000 1100 |
Binary OR Operator copies a bit if it exists in either operand. | ||
(a | b) will give 61, which is 0011 1101 | ^ |
Binary XOR Operator copies the bit if it is set in one operand but not both. | (a ^ b) will give 49, which is 0011 0001 | ~ |
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. | (~a ) will give -61, which is 1100 0011 in 2’s complement form due to a signed binary number. | << |
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | a << 2 will give 240, which is 1111 0000 | >> |
Rubyの論理演算子
以下の論理演算子がRuby言語でサポートされています
変数_a_が10を保持し、変数_b_が20を保持すると仮定します-
Operator | Description | Example |
---|---|---|
and | Called Logical AND operator. If both the operands are true, then the condition becomes true. | (a and b) is true. |
or | Called Logical OR Operator. If any of the two operands are non zero, then the condition becomes true. | (a or b) is true. |
&& | Called Logical AND operator. If both the operands are non zero, then the condition becomes true. | (a && b) is true. |
Called Logical OR Operator. If any of the two operands are non zero, then the condition becomes true. | (a | |
b) is true. | ! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. |
!(a && b) is false. | not | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. |
ルビー三項演算子
Ternary Operatorと呼ばれるもう1つの演算子があります。 最初に式の真または偽の値を評価し、評価の結果に応じて2つの指定されたステートメントのいずれかを実行します。 条件演算子はこの構文を持っています-
Operator | Description | Example |
---|---|---|
? : | Conditional Expression | If Condition is true ? Then value X : Otherwise value Y |
Rubyの範囲演算子
Rubyのシーケンス範囲は、連続する値の範囲を作成するために使用されます-開始値、終了値、およびその間の値の範囲で構成されます。
Rubyでは、これらのシーケンスは「..」および「…」範囲演算子を使用して作成されます。 2ドット形式は包括的範囲を作成し、3ドット形式は指定された高い値を除外する範囲を作成します。
Operator | Description | Example |
---|---|---|
.. | Creates a range from start point to end point inclusive. | 1..10 Creates a range from 1 to 10 inclusive. |
… | Creates a range from start point to end point exclusive. | 1…10 Creates a range from 1 to 9. |
ルビ定義? オペレータ
定義済み? は、渡された式が定義されているかどうかを判断するメソッド呼び出しの形式をとる特別な演算子です。 式の説明文字列、または式が定義されていない場合は_nil_を返します。
定義のさまざまな使用法がありますか? オペレーター
使い方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)
ルビードット「。」および二重コロン「::」演算子
モジュールの名前とピリオドを名前の前に付けてモジュールメソッドを呼び出し、モジュール名と2つのコロンを使用して定数を参照します。
*::* は単項演算子であり、クラスまたはモジュール内で定義された定数、インスタンスメソッド、およびクラスメソッドに、クラスまたはモジュール外のどこからでもアクセスできます。
*Remember* では、クラスとメソッドも定数と見なされる場合があります。
*::* Const_nameの前に、適切なクラスまたはモジュールオブジェクトを返す式を付ける必要があります。
プレフィックス式が使用されていない場合、デフォルトでメインのObjectクラスが使用されます。
ここに2つの例があります-
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
第二の例
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演算子の優先順位
次の表に、優先順位の高いものから低いものまで、すべての演算子を示します。
Method | Operator | Description |
---|---|---|
Yes | :: | Constant resolution operator |
Yes | [ ] [ ]= | Element reference, element set |
Yes | ** | Exponentiation (raise to the power) |
Yes | ! ~ PLUS - | Not, complement, unary plus and minus (method names for the last two are PLUSCOMMAT and -COMMAT) |
Yes | */% | Multiply, divide, and modulo |
Yes | PLUS - | Addition and subtraction |
Yes | >> << | Right and left bitwise shift |
Yes | & | Bitwise 'AND' |
Yes | ^ | |
Bitwise exclusive `OR' and regular `OR' | Yes | ⇐ < > >= |
Comparison operators | Yes | <⇒ == === != =~ !~ |
Equality and pattern match operators (!= and !~ may not be defined as methods) | && | |
Logical 'AND' | ||
Logical 'OR' | ||
.. … | Range (inclusive and exclusive) | |
? : | Ternary if-then-else | |
= %= \{/= -= PLUS= | = &= >>= <⇐* = &&= | |
= **= | Assignment | |
defined? | Check if specified symbol defined | |
not | Logical negation | |
or and | Logical composition |
注-メソッド列に_Yes_が含まれる演算子は、実際にはメソッドであるため、オーバーライドされる場合があります。