ruby推送示例_Ruby| 示例范围

ruby推送示例

Ruby山脉 (Ruby Ranges)

Ranges are the operators which are symbolized as ".." and "..." and have a start point and an endpoint. You can call them double dot and triple dot operators respectively. The difference between both the operators is that double dot operator is the inclusive range operator and triple dot is an exclusive range operator which means that if you print (1..4), it will print numbers from 1 to 4 whereas if you print (1…4), it will print numbers from 1 to 3. Ranges are used to decrease the code size and to increase the flexibility of the Ruby code.

范围是符号为“ ..”“ ...”的运算符,具有起点和终点。 您可以分别将它们称为双点和三点运算符。 两种运算符的区别在于,双点运算符是包含范围的运算符,三点运算符是唯一范围的运算符,这意味着如果您打印(1..4),它将打印从1到4的数字,而如果您打印( 1…4),它将打印从1到3的数字。范围用于减小代码大小并提高Ruby代码的灵活性。

范围类型 (Types of Ranges)

You get three types of Ranges in Ruby namely:

您在Ruby中获得三种类型的Ranges:

  1. Ranges as sequences

    范围作为序列

  2. Ranges as intervals

    间隔范围

  3. Ranges as conditions

    条件范围

Now, let us discuss them with examples for a better understanding of the concept.

现在,让我们通过示例进行讨论,以更好地理解该概念。

1)范围为序列 (1) Ranges as sequences)

When you use Ranges as a sequence, you are trying to get successive values in a particular sequence. You are supposed to specify a starting point in the form of an integer and an endpoint. You can use the double dot operator as well as the triple-dot operator as per the requirement of your code.

当您将范围用作序列时,您尝试获取特定序列中的连续值。 您应该以整数和端点的形式指定起点。 您可以根据代码要求使用双点运算符和三点运算符。

Let us understand its implementation with the help of example given below:

让我们借助以下示例了解其实现:

=begin
Ruby program to demonstrate 
ranges as sequences using .. operator.
=end

myrange = 4..8

maxv = myrange.max 
puts "Maximum value from range = #{maxv}"

minv = myrange.min 
puts "Minimum value from range = #{minv}"
	
if myrange.include?(8) then
	puts "8 is present"
end

myrange.each do |digit| 
    puts "#{digit} * #{digit} = #{digit*digit}"
end

Output

输出量

Maximum value from range = 8
Minimum value from range = 4
8 is present
4 * 4 = 16
5 * 5 = 25
6 * 6 = 36
7 * 7 = 49
8 * 8 = 64

In the above code, first we are printing maximum and minimum value out of range and then we are checking the presence of a particular element. In the end, we are printing all the elements of the range.

在上面的代码中,首先我们打印出最大值和最小值超出范围,然后我们检查特定元素的存在。 最后,我们将打印范围的所有元素。

Now, let us look at the same example with the help of triple dot operator "...".

现在,让我们借助三点运算符“ ...”查看相同的示例。

=begin
Ruby program to demonstrate 
ranges as sequences using ... operator.
=end

myrange = 4...8

maxv = myrange.max 
puts "Maximum value from range = #{maxv}"

minv = myrange.min 
puts "Minimum value from range = #{minv}"
	
if myrange.include?(8) then
	puts "8 is present"
else
	puts "8 is not present"
end
myrange.each do |digit| 
    puts "#{digit} * #{digit} = #{digit*digit}"
end

Output

输出量

Maximum value from range = 7
Minimum value from range = 4
8 is not present
4 * 4 = 16
5 * 5 = 25
6 * 6 = 36
7 * 7 = 49

Now, you can see that when we use the triple-dot operator, it is not including the endpoint in the range.

现在,您可以看到,当我们使用三点运算符时,它不在范围内。

2)范围作为条件 (2) Ranges as Condition)

You can also use range as a conditional statement inside a loop. We often use it with for loop. Refer the example given below:

您还可以将range用作循环内的条件语句。 我们经常将其与for循环配合使用。 请参考以下示例:

=begin
Ruby program to demonstrate 
ranges as conditions using ... operator.
=end

puts "Enter start point:-"
start = gets.chomp.to_i

puts "Enter end point:-"
endpoint = gets.chomp.to_i

puts "--------------"
for i in start..endpoint
    puts i*10
end

Output

输出量

Enter start point:-
10
Enter end point:-
20
--------------
100
110
120
130
140
150
160
170
180
190
200

You can also implement range as a condition in the switch case statement. See the example given below:

您还可以在switch case语句中将范围实现为条件。 请参见下面的示例:

=begin
Ruby program to demonstrate 
ranges as conditions using .. operator.
=end

puts "Enter the number"
num = gets.chomp.to_i	

rslt = case num
when 1000..2000 then "Between 1000 and 2000"
when 2000..3000 then "Between 2000 and 3000"
when 4000..5000 then "Between 4000 and 5000"
when 6000..7000 then "Between 6000 and 7000"
else "Above 7000"
end

puts rslt

Output

输出量

Enter the number
1200
Between 1000 and 2000

You can observe that when the user enters 1200, it prints 'Between 1000 and 2000' because 1200 lies between 1000 and 2000.

您可以观察到,当用户输入1200时,由于1200位于1000和2000之间,因此它会打印“介于1000和2000之间”。

3)区间为间隔 (3) Ranges as Intervals)

You can also use ranges to check the presence of a number in the range. We use "===" operator in this case. See the following code:

您还可以使用范围来检查范围中是否存在数字。 在这种情况下,我们使用“ ===”运算符 。 请参见以下代码:

=begin
Ruby program to demonstrate 
ranges as intervals using .. operator.
=end

puts "Enter the Alphabet"
alpha = gets.chomp

if (('A'..'T') === alpha) 
    puts "#{alpha} lies in the range of A to T"
else
    puts "#{alpha} does not lies in the range of A to T"
end

Output

输出量

RUN 1:
Enter the Alphabet
V
V does not lies in the range of A to T

RUN 2:
Enter the Alphabet
F
F lies in the range of A to T

You can observe in the above code that we are using === operator to implement range as an interval.

您可以在上面的代码中观察到我们正在使用===运算符将范围实现为间隔。

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

ruby推送示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值