ruby 三元表达式_Ruby其他表达式

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.

现在,所有三个变量都具有相同的值。

chaining assignments in ruby

You can see that the variables a, b & c has the same value 0.

您可以看到变量abc具有相同的值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 。 我们现在可以使用定义的操作符来识别什么

Defined opertor in Ruby

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
Parallel Assignment in Ruby 1

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来完成。 安静简单。 对?

Parallel Assignment in Ruby 2

Initially the values of a and b is 5 and 10 and after parallel assignment, the values of a and b is swapped.

最初, ab的值是510,并在并行分配后swapped ab的值。

翻译自: https://www.studytonight.com/ruby/miscellaneous-expressions-in-ruby

ruby 三元表达式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值