ruby 三目运算符_Ruby运算符

ruby 三目运算符

Ruby运算符 (Ruby operators)

Operators are the symbols which assist compiler or interpreter to carry out certain mathematical, logical and relational tasks and produce the results. Operators are method calls with parameters.

运算符是帮助编译器或解释器执行某些数学,逻辑和关系任务并产生结果的符号。 运算符是带有参数的方法调用。

Ruby运算符的类型 (Types of Ruby operators)

Ruby supports a variety of operators but following are few of them:

Ruby支持多种运算符,但其中很少有:

  1. Arithmetic operators

    算术运算符

  2. Comparison operators

    比较运算符

  3. Assignment operators

    赋值运算符

  4. Bitwise operators

    按位运算符

1)算术运算符 (1) Arithmetic Operators)

They are mathematical methods that takes two parameters and perform arithmetic operations on them. Following is the list of arithmetic operators,

它们是采用两个参数并对它们执行算术运算的数学方法。 以下是算术运算符列表,

SymbolNameDescription
+AdditionIt Adds two provided values
-SubtractionIt Subtracts two given values
/DivisionUsed to Divide left hand operand by right hand operand
*MultiplicationUsed to multiply operands present on both the sides.
%ModulusGives remainder as result by dividing left hand side operand with the right hand side operand.
**ExponentIt is used to carry out power calculations on operands.
符号 名称 描述
+ 加成 它添加两个提供的值
-- 减法 将两个给定值相减
/ 用于将左操作数除以右操作数
* 乘法 用于将两侧的操作数相乘。
模量 通过将左侧操作数除以右侧操作数得到余数。
** 指数 用于对操作数进行幂计算。

Example:

例:

puts "Enter first value"
num1 = gets.chomp.to_i
puts "Enter second value"
num2 = gets.chomp.to_i
add=num1+num2
puts "Sum is #{add}"
sub=num1-num2
puts "Difference is #{sub}"
mul=num1*num2
puts "Product  is #{mul}"
div=num1/num2
puts "Division is #{div}"
mod=num1%num2
puts "Remainder is #{mod}"

Output

输出量

Enter first value  
10  
Enter second value 
3
Sum is 13 
Difference is 7 
Product  is 30  
Division is 3
Remainder is 1 

2)比较运算符 (2) Comparison Operator)

They are a variant of binary operators which takes two operands and carry out comparison between them.

它们是二进制运算符的变体,它接受两个操作数并在它们之间进行比较。

Following is the list of comparison operators,

以下是比较运算符的列表,

SymbolDescription
==It is used to check whether the two specified operands are equal or not. If yes, then condition becomes true.
!=When there is a need to check whether the values of operands are not equal to each other, then this operator is used.
>Used to check if the left hand side value is greater than right hand side operand, if yes then the condition becomes true.
<Used to check if the right hand side value is greater than left hand side operand, if yes then the condition becomes true.
>=Used to check if the left hand side value is greater than or equal to right hand side operand, if yes then the condition becomes true.
<=Used to check if the right hand side value is greater than or equal to left hand side operand, if yes then the condition becomes true.
<=>It returns 0 if first value is equal second value, 1 if first operand is greater than the second operand and -1 if first operand is less than the later one.
.eql?It returns true if the receiver and the arguments are of same data type and have equal values.
.equal?It returns true if the receiver and the arguments hail from same object id.
符号 描述
== 用于检查两个指定的操作数是否相等。 如果是,则条件变为真。
!= 当需要检查操作数的值是否彼此不相等时,将使用此运算符。
> 用于检查左侧值是否大于右侧操作数,如果是,则条件变为true。
< 用于检查右侧值是否大于左侧操作数,如果是,则条件为真。
> = 用于检查左侧值是否大于或等于右侧操作数,如果是,则条件变为true。
<= 用于检查右侧值是否大于或等于左侧操作数,如果是,则条件变为true。
<=> 如果第一个值等于第二个值,则返回0;如果第一个操作数大于第二个操作数,则返回1;如果第一个操作数小于第二个操作数,则返回-1。
.eql? 如果接收方和参数的数据类型相同且值相等,则返回true。
。等于? 如果接收方和参数来自相同的对象ID,则返回true。

Example:

例:

puts "Enter first value"
num1 = gets.chomp.to_i
puts "Enter second value"
num2 = gets.chomp.to_i
if num1==num2
puts "Both are equal"
elsif num1>num2
puts "num1 is greater than num2"
elsif num1<num2
puts "num2 is greater than num1"
elsif num1!=num2
puts "num2 is not equal to num1"
elsif num1>=num2
puts "num1 is greater than or equals to num2"
elsif num1<=num2
puts "num2 is greater than or equals to num1"
end

Output

输出量

Enter first value
100
Enter second value 
231
num2 is greater than num1 

3)赋值运算符 (3) Assignment operators)

Assignment operators are used to assign the value of right hand side operator to the left hand side operator.

赋值运算符用于将右侧运算符的值分配给左侧运算符。

Following is the list of assignment operators which are supported in Ruby,

以下是Ruby支持的赋值运算符的列表,

OperatorDescription
=Used to assign right hand value to the left hand operand.
+=Adds left hand operand with the right hand operand and stores the result in left hand operand.
-=Subtracts right hand operand from the left hand operand and stores the result in left hand operand.
*=Multiplies left hand operand with the right hand operand and stores the product in left hand operand.
%=Used to find modulus by dividing and stores remainder in left hand operand.
/=Used to find division and stores result in left hand operand
**=Used to find exponential power and keeps the result in the left operand after computation.
操作员 描述
= 用于将右手值分配给左手操作数。
+ = 将左操作数与右操作数相加,并将结果存储在左操作数中。
-= 从左侧操作数中减去右侧操作数,并将结果存储在左侧操作数中。
* = 将左操作数与右操作数相乘,并将乘积存储在左操作数中。
%= 用于通过除法求模并将余数存储在左操作数中。
/ = 用于查找除法并将结果存储在左操作数中
** = 用于查找指数幂并在计算后将结果保留在左操作数中。

Example:

例:

puts "Enter first value"
num1 = gets.chomp.to_i
puts "Enter second value"
num2 = gets.chomp.to_i
puts "Enter choice:"
ch = gets.chomp
case ch
when 'a'
num1+=1
puts "#{num1}"
when 'b'
num2-=2
puts "#{num2}"
when 'c'
num1*=2
puts "#{num1}"
when 'd'
num1**=9
puts "#{num1}"
else
puts "option not available."	
end

Output

输出量

Enter first value
10
Enter second value
2
Enter choice:
c
20

4)按位运算符 (4) Bitwise operators)

They are used to perform bitwise operations on binary number which includes change of particular bit.

它们用于对二进制数执行按位运算,其中包括更改特定位。

Following is the list of bitwise operators which are supported by Ruby,

以下是Ruby支持的按位运算符列表,

SymbolNameDescription
&Binary ANDIt works by copying a bit to the result if it is present in both the operands
|Binary ORIt works by copying a bit to the result if it is present in one of the operands.
^Binary XORIt works by copying a bit to the result if it is present in one of the operand but not in both.
~Binary 1's complementIt flips the bits like making 1 to 0 and vice versa.
>>Binary left shiftIt shifts the bit to the left by number of bits told by the left operand.
<<Binary Right shiftIt shifts the bit to the right by number of bits told by the right operand.
符号 名称 描述
二进制与 如果两个操作数中都存在一个位,则将其复制到结果中
| 二进制或 它通过将一个位复制到结果(如果其中一个操作数中存在)来工作。
^ 二进制异或 它通过将一个位复制到结果(如果在一个操作数中但在两个操作数中都不存在)来工作。
二进制1的补码 它会将位翻转为1到0,反之亦然。
>> 二进制左移 它将左边操作数告知左边操作数的位数。
<< 二进制右移 它将右边的操作数告知右边的位数。

Example:

例:

puts "Enter first value"
num1 = gets.chomp.to_i
puts "Enter second value"
num2 = gets.chomp.to_i
a1=num1 & num2
puts "and is #{a1}"
a2=num1 | num2
puts "OR is #{a2}"
a3=num1^num2
puts "XOR is #{a3}"
a4= ~num2
puts "Complement is #{a4}"
a5=num1>>num2
puts "left shift is #{a5}"
a6=num1<<num2
puts "Right shift is #{a6}"

Output

输出量

Enter first value
3
Enter second value 
5
and is 1 
OR is 7
XOR is 6 
Complement is -6 
left shift is 0
Right shift is 96

翻译自: https://www.includehelp.com/ruby/operators.aspx

ruby 三目运算符

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543713.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

极验验证码流程-3.图片加密处理 图片移位

终于把图片加密给搞定了&#xff0c;原理是他把图分成了52个部分&#xff0c;然后通过移动来形成新的图片 主要的位置关系看代码 顺便吐槽下ruby,小众语言就是这么不方便&#xff0c;很多库都没有&#xff0c;百度了半天 最后换成了java来写 图片保存到本地的就不详细说了 主要…

什么是Brouter?

代理&#xff1a;网络设备 (Brouter: A network device) Brouter is a network device, which operates as a combination of both bridge and router. In this single device, a user will get a function of both bridge and router, as it can send out data to create a co…

11gR2 RAC时间同异常导致节点down掉问题处理

实验环境下11204的RAC环境&#xff0c;出现了一个节点DOWN掉的问题。检查日志信息后&#xff0c;在otcssd日志信息发现如下信息&#xff1a;2016-01-17 23:15:20.564: [ CTSS][1175029504]ctsscomm_recv_cb2: Receive incoming message event. Msgtype [3].2016-01-17 23:15…

html-iframe_HTML iframe

html-iframeiframe (Iframes) In HTML, iframes are used to display a webpage inside another webpage. 在HTML中&#xff0c; iframe用于在另一个网页内显示一个网页。 Syntax: 句法&#xff1a; <iframe src"URL"></iframe>The <iframe> tag…

ruby循环_Ruby循环

ruby循环Ruby循环 (Ruby Loops) Loops are comprised of sequentially group instructions which are executed repeatedly until a certain condition is met. Loops provide great help in making the programmers task easier. 循环由顺序执行的组指令组成&#xff0c;这些指…

后缀数组 TYVJ P1860 后缀数组

/*P1860 后缀数组时间: 1000ms / 空间: 131072KiB / Java类名: Main描述 我们定义一个字符串的后缀suffix(i)表示从s[i]到s[length(s)]这段子串。后缀数组&#xff08;Suffix array&#xff09;SA[i]中存放着一个排列&#xff0c;满足suffix(sa[i])<suffix(sa[i1]) 按照字典…

Binary XML file line #2: You must supply a layout_height attribute inflate

Android开发中遇到的奇葩问题之一&#xff1a;java.lang.NullPointerException&#xff0c;java.lang.RuntimeException:Binary XML file line #2: You must supply a layout_height attribute inflate&#xff0c;遇到这个问题说明你在非主流上测试&#xff0c;或者说是在部分…

量词逻辑量词里面的v表示?_知识表示能力问答中的人工智能量词(MCQ)

量词逻辑量词里面的v表示&#xff1f;1) How many types of quantifiers are there that are used to represent knowledge? 3 types2 typesUser can define as many quantifiers he wantsNone of the above Answer & Explanation Correct answer: 22 types There are two…

给定数组A []和数字X,请检查A []中是否有对X | 使用两个指针算法,O(1)空间复杂度| 套装2...

Prerequisite: 先决条件&#xff1a; Hashing data structure 散列数据结构 Given an array A[] and number X, check for pair in A[] with sum X | using hashing O(n) time complexity | Set 1 给定数组A []和数字X&#xff0c;请检查A []中是否有对X | 使用哈希O(n)时间复…

集合操作(三)Set

2019独角兽企业重金招聘Python工程师标准>>> Set集合 HashSet 哈希表保证元素的唯一性依赖于两个方法一个是hashCode方法一个是equals方法 如果两个对象的hashCode值相同,并且调用该对象的equals方法返回的是true的时候,那么就说明两个对象是相同的 结论&#xff1a…

mcq 队列_MCQ | 软件程序分析工具和组件分类| 免费和开源软件

mcq 队列Q1. Which of the following analysis methods come under Static Analysis Tools? Q1。 静态分析工具包含以下哪些分析方法&#xff1f; Code Walkthrough 代码演练 Code Inspection 代码检查 None of the Above 以上都不是 Both a. & b. 两者都 &#xff06;b。…

samba部署小结

[rootOracle ~]# yum install samba-swat -y[rootOracle ~]# yum install samba-client 客户端工具主配置文件&#xff1a;[rootOracle ~]# cat /etc/samba/smb.conf |grep -v "#"|grep -v "^$"|grep -v ";"[global]workgroup …

JAVA调用动态链接库

上一篇《JAVA本地接口&#xff08;JNI&#xff09;》中介绍了JAVA的JNI技术&#xff0c;通过JAVA自有的方式调用动态链接库&#xff0c;这一篇将继续为大家介绍使用其他方式调用动态链接库。 首先&#xff0c;我们编写一个用于测试的链接库 头文件 print.h #ifdef DLL_IMPLEME…

数组重复次数最多的元素递归_在不使用递归的情况下计算链接列表中元素的出现次数...

数组重复次数最多的元素递归Solution: 解&#xff1a; Input: 输入&#xff1a; A singly linked list whose address of the first node is stored in a pointer, say head and key is the data of which we have to count the number of occurrences. 一个单链表 &#xff…

DshanMCU-R128s2芯片外设支持列表

LCD 显示屏 厂商分辨率型号接口FPS100ask480 x 320Dshan_Display ModuleSPI60 摄像头 Sensor 厂商分辨率型号Size接口FPSGalaxyCoreVGA, 640 x 480GC03081/6.5DVP30GalaxyCoreUXGA, 1616 x 1232GC21451/5DVP13

第6周 搜索与排序

1 查找里程 给你这样一张里程表&#xff0c;如何写一个程序&#xff0c;输入两地的地名&#xff0c;能输出期间的里程&#xff1f; #include <stdio.h> #include <string.h> #define C_LEN 30typedef struct city {char name1[C_LEN];char name2[C_LEN];int distan…

(转) Twisted :第十九部分 改变之前的想法

2019独角兽企业重金招聘Python工程师标准>>> 简介 Twisted是一个正在进展的项目,它的开发者会定期添加新的特性并且扩展旧的特性. 随着Twisted 10.1.0发布,开发者向 Deferred 类添加了一个新的特性—— cancellation ——这正是我们今天要研究的. 异步编程将请求和响…

stl list 删除元素_删除所有出现的元素,并从列表中删除一些特定的元素。 C ++ STL...

stl list 删除元素list.remove()和list.remove_if()函数 (list.remove() and list.remove_if() functions) remove() function is used to remove all occurrences of a given element from the list and function remove_if() is used to remove set of some specific element…

Mac 获取 Brew

2019独角兽企业重金招聘Python工程师标准>>> 终端输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 转载于:https://my.oschina.net/fdstudio/blog/610680

express 项目生成器_用于项目的Express模板生成器(2)| 应用程序结构研究

express 项目生成器Hello! In express template generator for your projects (1), we looked at express generator and how we can start an express application with stressing to build a brand new structure of all required files. 你好&#xff01; 在针对您的项目的E…