红宝石 |运营商
运算符是一个符号,表示要使用一个或多个操作数执行的操作。运算符是任何编程语言的基础。运算符允许我们对操作数执行不同类型的操作。 Ruby 中使用了不同类型的运算符,如下所示:
算术运算符
这些用于对操作数执行算术/数学运算。
- Addition(+):运算符将两个操作数相加。例如, x+y 。
- 减法(-):运算符减去两个操作数。例如, xy 。
- 乘法(*):运算符将两个操作数相乘。例如, x*y 。
- 除法(/):运算符将第一个操作数除以第二个操作数。例如, x/y 。
- Modulus(%):运算符返回第一个操作数除以第二个操作数时的余数。例如, x%y 。
- 指数(**):运算符返回操作数的指数(幂)。例如, x**y 。
例子:
Ruby
# Ruby program to demonstrate
# the Arithmetic Operators
# Addition
puts ("Addition:")
puts (10 + 20)
# Subtraction
puts ("Subtraction:")
puts (40 - 20)
# Division
puts ("Division:")
puts (100 / 20)
# Multiplication
puts ("Multiplication:")
puts (10 * 20)
# Modulus
puts ("Modulus:")
puts (20 % 7)
# Exponent
puts ("Exponent:")
puts (2 ** 4)
Ruby
# Ruby program to demonstrate
# the Comparison Operators
puts "Equal To Operator:"
puts (10 == 20)
puts "Not Equal To Operator:"
puts (40 != 20)
puts "Greater than Operator"
puts (100 > 20)
puts "Less than Operator"
puts (10 < 20)
puts "Less than Equal To Operator"
puts (2 <= 5)
puts "Greater than Equal To Operator"
puts (2 >= 5)
puts "Combined combination operator"
puts(20 <=> 20)
puts(10 <=> 20)
puts(20 <=> 10)
Ruby
# Ruby program to demonstrate
# the Logical Operators
# Variables
a = 10
b = 20
c = 30
# using && operator
if a == 10 && b == 20 && c == 30
puts "Logical AND Operator"
puts result = a * b * c
end
# using || operator
puts "Logical OR operator"
if a == 10 || b == 20
puts result = a + b + c
end
# using ! operator
puts "Logical Not Operator"
puts !(true)
Ruby
# Ruby program to demonstrate
# the Assignments Operators
puts "Simple assignment operator"
puts a = 20
puts "Add AND assignment operator"
puts a += 10
puts "Subtract AND assignment operator"
puts a -= 5
puts "Multiply AND assignment operator"
puts a *= 10
puts "Divide AND assignment operator"
puts a /= 4
puts "Modulus AND assignment operator"
puts a %= 3
puts "Exponent AND assignment operator"
puts a **= 3
Ruby
# Ruby program to demonstrate
# the Bitwise Operators
# variables
a = 10
b = 20
puts "Bitwise AND operator"
puts (a & b)
puts "Bitwise OR operator"
puts (a |b)
puts "Bitwise XOR operator"
puts (a ^ b)
puts "Bitwise Complement operator"
puts (~a)
puts "Binary right shift operator"
puts (a >> 2)
puts "Binary left shift operator"
puts (a << 2)
Ruby
# Ruby program to demonstrate
# the Ternary Operator
# variable
marks_obtained = 100
# using ternary operator
result = marks_obtained > 40 ? 'Pass' : 'Fail'
# displaying output
puts result
Ruby
# Ruby program to demonstrate
# the Range Operator
# Array value separator
$, =", "
# using .. Operator
range_op = (7 .. 10).to_a
# displaying result
puts "#{range_op}"
# using ... Operator
range_op1 = (7 ... 10).to_a
# displaying result
puts "#{range_op1}"
Ruby
# Ruby program to demonstrate
# the defined? Operator
# variables
GFG = 1
Geeks = 70
puts ("define? Operator Results")
# using defined? Operator
# it returns constant
puts defined? GFG
# it returns constant
puts defined? Geeks
# it returns expression
puts defined? a
# it returns expression
puts defined? 50
Ruby
# Ruby program to demonstrate
# Dot “.” and Double Colon
# “::” Operators
# defined constant on main Object class
CONS = 5
# define module
module Geeks
CONS = 5
# set global CONS to 7
::CONS = 7
# set local CONS to 10
CONS = 10
end
# displaying global CONS value
puts CONS
# displaying local "Geeks" CONS value
# using :: operator
puts Geeks::CONS
class Gfg
def Geeks2
puts "Dot Operator"
end
end
# calling Geeks2 module using
# Dot(.) operator
puts Gfg.new.Geeks2
输出:
Addition:
30
Subtraction:
20
Division:
5
Multiplication:
200
Modulus:
6
Exponent:
16
比较运算符
运算符或关系运算符用于比较两个值。让我们一一看看:
- Equal To(==)运算符检查两个给定操作数是否相等。如果是,则返回 true。否则返回false。例如, 5==5将返回 true。
- Not Equal To(!=)运算符检查两个给定的操作数是否相等。如果不是,则返回 true。否则返回false。它是'=='运算符的精确布尔补码。例如, 5!=5将返回 false。
- 大于(>)运算符检查第一个操作数是否大于第二个操作数。如果是,则返回 true。否则返回false。例如, 6>5将返回 true。
- 小于(<)运算符检查第一个操作数是否小于第二个操作数。如果是,则返回 true。否则返回false。例如, 6<5将返回 false。
- 大于等于 (>=)运算符检查第一个操作数是否大于或等于第二个操作数。如果是,则返回 true。否则返回false。例如, 5>=5将返回 true。
- 小于等于 (<=)运算符检查第一个操作数是否小于或等于第二个操作数。如果是,则返回 true。否则返回false。例如, 5<=5也将返回 true。
- 组合组合 (<=>)运算符在第一个操作数等于第二个操作数时返回 0,当第一个操作数大于第二个操作数时返回 1,当第一个运算符数小于第二个操作数时返回 -1。
- Case Equality Operator(===)它将测试 case 语句中的相等性。
- '.eql?'如果接收者和参数具有相同的类型和相等的值,则此运算符返回 true。
- '平等的?'如果接收者和参数具有相同的对象 ID,则此运算符返回 true。
例子:
红宝石
# Ruby program to demonstrate
# the Comparison Operators
puts "Equal To Operator:"
puts (10 == 20)
puts "Not Equal To Operator:"
puts (40 != 20)
puts "Greater than Operator"
puts (100 > 20)
puts "Less than Operator"
puts (10 < 20)
puts "Less than Equal To Operator"
puts (2 <= 5)
puts "Greater than Equal To Operator"
puts (2 >= 5)
puts "Combined combination operator"
puts(20 <=> 20)
puts(10 <=> 20)
puts(20 <=> 10)
输出:
Equal To Operator:
false
Not Equal To Operator:
true
Greater than Operator
true
Less than Operator
true
Less than Equal To Operator
true
Greater than Equal To Operator
false
Combined combination operator
0
-1
1
逻辑运算符
它们用于组合两个或多个条件/约束或补充考虑的原始条件的评估。它们描述如下:
- 当两个条件都满足时,逻辑 AND(&&)运算符返回 true。否则返回false。使用“和”是 &&运算符的替代方法。例如,当 a 和 b 都为真(即非零)时, a && b返回真。
- 当考虑的一个(或两个)条件满足时,逻辑 OR(||)运算符返回 true。否则返回false。使用“或”是 || 的替代方法。运算符。例如,一个 ||如果 a 或 b 之一为真(即非零),则b返回真。当然,当 a 和 b 都为真时,它返回真。
- 逻辑 NOT(!):运算符在考虑的条件不满足时返回 true。否则返回false。使用“ not ”是 !运算符。例如, !true返回 false。
例子:
红宝石
# Ruby program to demonstrate
# the Logical Operators
# Variables
a = 10
b = 20
c = 30
# using && operator
if a == 10 && b == 20 && c == 30
puts "Logical AND Operator"
puts result = a * b * c
end
# using || operator
puts "Logical OR operator"
if a == 10 || b == 20
puts result = a + b + c
end
# using ! operator
puts "Logical Not Operator"
puts !(true)
输出:
Logical AND Operator
6000
Logical OR operator
60
Logical Not Operator
false
赋值运算符
赋值运算符用于为变量赋值。赋值运算符的左侧操作数是一个变量,而赋值运算符符的右侧操作数是一个值。右侧的值必须与左侧变量的数据类型相同,否则编译器将引发错误。
不同类型的赋值运算符如下所示:
- 简单赋值(=) :运算符是最简单的赋值运算符。该运算符用于将右侧的值赋给左侧的变量。
- Add AND Assignment (+=)运算符用于将左操作数与右操作数相加,然后将其分配给左侧的变量。
- 减与赋值 (-=)运算符用于将左操作数与右操作数相减,然后将其分配给左侧的变量。
- 乘与赋值 (*=)运算符用于将左操作数与右操作数相乘,然后将其分配给左侧的变量。
- 除法和赋值(/=)运算符用于将左操作数与右操作数相除,然后将其分配给左侧的变量。
- 模数与赋值 (%=)运算符用于将左操作数与右操作数进行模数赋值,然后将其赋值给左边的变量。
- 指数与赋值 (**=)运算符用于将左操作数的幂提高到右操作数并将其分配给左侧的变量。
例子:
红宝石
# Ruby program to demonstrate
# the Assignments Operators
puts "Simple assignment operator"
puts a = 20
puts "Add AND assignment operator"
puts a += 10
puts "Subtract AND assignment operator"
puts a -= 5
puts "Multiply AND assignment operator"
puts a *= 10
puts "Divide AND assignment operator"
puts a /= 4
puts "Modulus AND assignment operator"
puts a %= 3
puts "Exponent AND assignment operator"
puts a **= 3
输出:
Simple assignment operator
20
Add AND assignment operator
30
Subtract AND assignment operator
25
Multiply AND assignment operator
250
Divide AND assignment operator
62
Modulus AND assignment operator
2
Exponent AND assignment operator
8
位运算符
在 Ruby 中,有 6 个位运算运算符,它们在位级别工作或用于执行逐位操作。以下是按位运算运算符:
- 按位与 (&)将两个数字作为操作数,并对两个数字的每一位进行与运算。仅当两个位都为 1 时,AND 的结果才为 1。
- 按位或 (|)将两个数字作为操作数,并对两个数字的每一位进行 OR。 OR 的结果为 1 两位中的任何一位为 1。
- 按位异或 (^)将两个数字作为操作数,并对两个数字的每一位进行异或。如果两个位不同,则异或的结果为 1。
- 左移 (<<)取两个数,左移第一个操作数的位,第二个操作数决定要移位的位数。
- 右移 (>>)取两个数,右移第一个操作数的位,第二个操作数决定移位的位数。
- Ones Complement (~)该运算符采用单个数字,用于执行 8 位的补码运算。
例子:
红宝石
# Ruby program to demonstrate
# the Bitwise Operators
# variables
a = 10
b = 20
puts "Bitwise AND operator"
puts (a & b)
puts "Bitwise OR operator"
puts (a |b)
puts "Bitwise XOR operator"
puts (a ^ b)
puts "Bitwise Complement operator"
puts (~a)
puts "Binary right shift operator"
puts (a >> 2)
puts "Binary left shift operator"
puts (a << 2)
输出:
Bitwise AND operator
0
Bitwise OR operator
30
Bitwise XOR operator
30
Bitwise Complement operator
-11
Binary right shift operator
2
Binary left shift operator
40
三元运算符
它是一个条件运算符,是 if-else 语句的简写版本。它有三个操作数,因此得名三元。它将根据布尔表达式的值返回两个值之一。
句法 :
condition ? first_expression : second_expression;
解释 :
condition: It be evaluated to true or false.
If the condition is true
first_expression is evaluated and becomes the result.
If the condition is false,
second_expression is evaluated and becomes the result.
例子 :
红宝石
# Ruby program to demonstrate
# the Ternary Operator
# variable
marks_obtained = 100
# using ternary operator
result = marks_obtained > 40 ? 'Pass' : 'Fail'
# displaying output
puts result
输出:
Pass
范围运算符
在 Ruby 中,范围运算符用于创建指定元素的指定序列范围。 Ruby 中有两个范围运算符,如下所示:
- 双点 (..)运算符用于创建指定的序列范围,其中开始和结束元素都将包含在内。例如,7 .. 10 将创建一个类似 7、8、9、10 的序列。
- 三点 (…)运算符用于创建指定的序列范围,其中只有起始元素将包含在内,结束元素将不包含在内。例如,7 .. 10 将创建一个类似 7、8、9 的序列。
例子:
红宝石
# Ruby program to demonstrate
# the Range Operator
# Array value separator
$, =", "
# using .. Operator
range_op = (7 .. 10).to_a
# displaying result
puts "#{range_op}"
# using ... Operator
range_op1 = (7 ... 10).to_a
# displaying result
puts "#{range_op1}"
输出:
[7, 8, 9, 10]
[7, 8, 9]
定义?操作员
定义的?运算符是一个特殊的运算符,用于检查传递的表达式是否已定义。如果未定义传递的参数,则返回nil ,否则返回定义该参数的字符串。
句法:
defined? expression_to_be_checked
例子:
红宝石
# Ruby program to demonstrate
# the defined? Operator
# variables
GFG = 1
Geeks = 70
puts ("define? Operator Results")
# using defined? Operator
# it returns constant
puts defined? GFG
# it returns constant
puts defined? Geeks
# it returns expression
puts defined? a
# it returns expression
puts defined? 50
输出:
define? Operator Results
constant
constant
expression
点“。”和双冒号“::”运算符
- 点 (.)运算符用于访问类的方法。
- 双冒号 (::)运算符用于将类或模块中定义的常量、类方法和实例方法访问到类或模块之外的任何位置。需要记住的重要一点是,类和方法在 Ruby 中可能被视为常量,并且在:: Const_name 前面加上返回适当类对象的表达式。如果没有使用前缀表达式,则默认使用主 Object 类。
例子:
红宝石
# Ruby program to demonstrate
# Dot “.” and Double Colon
# “::” Operators
# defined constant on main Object class
CONS = 5
# define module
module Geeks
CONS = 5
# set global CONS to 7
::CONS = 7
# set local CONS to 10
CONS = 10
end
# displaying global CONS value
puts CONS
# displaying local "Geeks" CONS value
# using :: operator
puts Geeks::CONS
class Gfg
def Geeks2
puts "Dot Operator"
end
end
# calling Geeks2 module using
# Dot(.) operator
puts Gfg.new.Geeks2
输出:
7
10
Dot Operator
main.rb:14: warning: already initialized constant CONS
main.rb:6: warning: previous definition of CONS was here
main.rb:17: warning: already initialized constant Geeks::CONS
main.rb:11: warning: previous definition of CONS was here