我是靠谱客的博主 怕黑水蜜桃,最近开发中收集的这篇文章主要介绍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 三元表达式

最后

以上就是怕黑水蜜桃为你收集整理的ruby 三元表达式_Ruby其他表达式的全部内容,希望文章能够帮你解决ruby 三元表达式_Ruby其他表达式所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部