ruby 执行函数_Ruby pop()函数

ruby 执行函数

Ruby中的pop()函数 (pop() function in Ruby)

If you are aware of the logic to create pop() function used in the data structure, then you are ready to understand the internal logic of pop() function which is already defined in Ruby’s library. The pop () function is used to delete or pop out the element from the stack. Likewise in Ruby also, the pop() function is used to pop out or delete the element from the stack.

如果你意识到逻辑的创建弹出(在数据结构中使用)功能 ,那么你就可以明白这是在Ruby的库中已定义的流行()函数的内在逻辑。 pop()函数用于从堆栈中删除或弹出元素。 同样在Ruby中, pop()函数用于从堆栈中弹出或删除元素。

The return type of pop() function is an array and it returns an array in which it is invoked along with the popped elements.

pop()函数的返回类型是一个数组,它返回一个数组,在其中将其与弹出的元素一起调用。

Syntax:

句法:

 pop(Elements)

Now let us go through the implementation of pop() function with the help of examples.

现在,让我们在示例的帮助下完成pop()函数的实现。

pop() function only accepts integer value. The value you specify as the argument is the number of elements which are to be popped from the end of the array. If you invoke pop() without an argument, then the end most element will be popped out implicitly.

pop()函数仅接受整数值。 您指定为参数的值是要从数组末尾弹出的元素数。 如果您在带参数的情况下调用pop() ,则最后一个元素将被隐式弹出。

Example 1:

范例1:

=begin
Ruby program to demonstrate pop() function.
=end

# Initializing arrays
Arr1 = [11, 12, 13, 14] 
Arr2 = ["x", "y", "z"] 
Arr3 = ["Includehelp", "C#", "Ruby"]
Arr4 = ["Kiwi","Banana","Orange","Papaya"] 
   
# Invoking pop() function with elements to be popped 
Arr1pop = Arr1.pop(1)                 
Arr2pop = Arr2.pop(1)                
Arr3pop = Arr3.pop(2)
Arr4pop = Arr4.pop(2)   
   
# Printing the array of storing popped element 
puts "#{Arr1pop}"
puts "#{Arr2pop}"
puts "#{Arr3pop}"
puts "#{Arr4pop}"

Output

输出量

[14]
["z"]
["C#", "Ruby"]
["Orange", "Papaya"]

Code logic:

代码逻辑:

We have created four arrays. We are calling the pop() function with different arrays which is returning the popped elements in the corresponding new array. You can see the reflection of all popped elements in the new array.

我们创建了四个数组。 我们用不同的数组调用pop()函数 ,该函数返回相应新数组中的弹出元素。 您可以看到新数组中所有弹出元素的反射。

Example 2:

范例2:

=begin
Ruby program to demonstrate pop() function.
=end

# Initializing arrays
Arr1 = [11, 12, 13, 14,15,16,17,18,19,20,21,22,23,24,25,26] 
   
# Invoking pop() function with the number of elements to be popped 
puts "#{Arr1.pop}"
puts "#{Arr1.pop(1)}"
puts "#{Arr1.pop(2)}"
puts "#{Arr1.pop(3)}"
puts "#{Arr1.pop(4)}"
puts "#{Arr1.pop(5)}"

#printing final array after popping elements
puts "#{Arr1}"

Output

输出量

26
[25]
[23, 24]
[20, 21, 22]
[16, 17, 18, 19]
[11, 12, 13, 14, 15]
[]

Code logic:

代码逻辑:

In the above example, we are invoking pop() function multiple times in the same array. It is removing elements with every calling of the function. You can observe that when we invoke the pop() function with no arguments, it is popping out the last element of the array. When you specify the number of elements to be popped out, it pops out the element from the last up to the index number specified.

在上面的示例中,我们在同一数组中多次调用pop()函数 。 它在每次调用函数时都删除了元素。 您可以观察到,当我们不带任何参数调用pop()函数时,它弹出了数组的最后一个元素。 当您指定要弹出的元素数时,它将弹出从最后到指定索引号的元素。

翻译自: https://www.includehelp.com/ruby/pop-function.aspx

ruby 执行函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值