ruby 变量类中范围

您所在的位置:网站首页 ruby变量类型 ruby 变量类中范围

ruby 变量类中范围

2024-01-18 03:07| 来源: 网络整理| 查看: 265

ruby 变量类中范围

Ranges are a sequence of data. For example, numbers 0 through 9 are a range. Letters a through z are a range. We can also define our own range.

范围是数据序列。 例如,数字09是一个范围。 字母az是一个range 。 我们还可以定义自己的范围。

rangestartvalue..endvalue

Example : 0..9 is a range that contains values from 0 through 9. Similarly 'a'..'z' is also a range with all the alphabets in it.

示例: 0..9是包含从0到9的值的range 。类似地, “ a” ..“ z”也是其中包含所有字母的range 。

You can also create your own range like 'aab'..'aae' it contains values aab, aac, aad, aae.

您还可以创建自己的范围,例如'aab'..'aae' ,其中包含值aabaacaadaae

Range example in ruby

Range is a flexible data type in Ruby. We can also call methods on range once it is defined.

Range是Ruby中的一种灵活的数据类型。 一旦定义了范围,我们也可以在范围上调用方法。

Range methods in ruby - min, max, include? Ruby:Range数据类型的方法 (Ruby: Methods for Range data type)

include() method is used to check whether the particular element is present in the range or not. If the element is present in the range, it returns true else it returns false.

include()方法用于检查特定元素是否在范围内。 如果该元素存在于范围内,则返回true否则返回false 。

min() method returns the minimum element of the range.

min()方法返回范围的最小值

max() method returns the maximum element of the range.

max()方法返回范围的最大元素。

each method is used to loop around a range and print all the elements of a range on the shell output.

each方法都用于在范围内循环并在shell输出上打印范围内的所有元素。

We can display each element/member of the range like this :

我们可以显示的每个元素/部件range是这样的:

letters.each { |letter| print(letter) } Accessing elemetns of Range in Ruby

In the above code, each member of the range 'a'..'z' stored in variable letters is copied to the variable letter in each iteration. This is done by using the symbol ||. Then, we can do whatever we want to do with that value. Here, we have just printed the values in the variable letter. Ranges are commonly used as conditions in loops.

在上面的代码中,存储在变量letters中的范围'a'..'z'的每个成员在每次迭代中均被复制到变量letter中。 这可以通过使用符号||完成。 。 然后,我们可以使用该值做任何我们想做的事情。 在这里,我们刚刚将值打印在变量letter 。 Ranges通常用作循环中的条件。

There is also another useful operator called case equality operator (===). Using this operator, we can determine that whether any particular element is present in the given range or not. If the element is present in the range it returns true, else returns false.

还有另一个有用的运算符,称为大小写相等运算符 (===) 。 使用此运算符,我们可以确定在给定范围内是否存在任何特定元素。 如果该元素存在于范围内,则返回true ,否则返回false

equality operator on Ranges in Ruby

Here the element 'c' is present in the variable letters, so it returns true. In the same way, digits is a variable that contains range from 0 through 9, and we are checking whether 100 is of the range using the case equality operator, it returns false indicating that 100 doesn't belong to this range.

这里的元素'c'存在于可变letters ,因此它返回true 。 同样, digits是包含0到9范围的变量,我们正在使用case equality operator检查100是否属于该范围,它返回false指示100不属于该范围。

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

ruby 变量类中范围



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3