概述
ruby 三元表达式
In this tutorial we will learn about chaining assignments, defined operator and parallel assignments.
在本教程中,我们将学习链接分配,定义的运算符和并行分配。
Ruby:链接分配 (Ruby: Chaining assignments)
Very Commonly, in a program you want to initialize a set of variables with a same value, typically 0.
非常常见的是,在程序中,您希望使用相同的值(通常为0)来初始化一组变量。
However, you can do this like
但是,您可以这样做
a = 0
b = 0
c = 0
But, there is another way called chaining assignments
used by programmers.
但是,程序员还有另一种称为chaining assignments
方式。
a = b = c= 0
Now all the three variables have the same value.
现在,所有三个变量都具有相同的值。
You can see that the variables a, b & c has the same value 0.
您可以看到变量a , b和c具有相同的值0。
Ruby :: defined
运算符 (Ruby:: defined
Operator)
This is an interesting feature of ruby. Using this in an expression we can determine what type of identifier it is. We have defined the variable a. We can now use defined operator to identify what is a?
这是Ruby的有趣特征。 在表达式中使用它可以确定标识符的类型。 我们已经定义了变量a 。 我们现在可以使用定义的操作符来识别什么是 ?
Since variable a is declared locally it returned as local-variable
. Likewise, it returned method
for printf and assignment
for the expression a = 1.
由于变量a是在本地声明的,因此返回为local-variable
。 同样,它返回printf的 method
并为表达式a = 1 assignment
。
Ruby:并行分配 (Ruby: Parallel Assignment)
Consider the variable a contains 5 and b contains 10 and now we have to swap the values of the variables.
考虑变量a包含5,b包含10,现在我们必须交换变量的值。
It is done by,
它是由
a = 5
b = 10
temp = a
a = b
b = temp
But there is an efficient way to do this called parallel assignment
. This can be done by a, b = b, a . Quiet simple. Right?
但是有一种有效的方法可以做到这一点,称为parallel assignment
。 这可以通过a,b = b,a来完成。 安静简单。 对?
Initially the values of a and b is 5 and 10 and after parallel assignment, the values of a and b is swapped
.
最初, a和b的值是5和10,并在并行分配后swapped
a和b的值。
翻译自: https://www.studytonight.com/ruby/miscellaneous-expressions-in-ruby
ruby 三元表达式
最后
以上就是怕黑水蜜桃为你收集整理的ruby 三元表达式_Ruby其他表达式的全部内容,希望文章能够帮你解决ruby 三元表达式_Ruby其他表达式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复