ruby 执行函数_Ruby push()函数

ruby 执行函数

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

You may have studied the logic to create the push() function in data structure manually which is used in Stack to push the element to the top of the stack. Likewise, in Ruby, we have a predefined library function known as push(), which is used to add the element or group of elements at the end of the array.

您可能已经研究了在数据结构中手动创建push()函数的逻辑,该逻辑在Stack中用于将元素推入堆栈的顶部。 同样,在Ruby中,我们有一个称为push()的预定义库函数,该函数用于在数组的末尾添加元素或元素组。

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

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

Syntax:

句法:

 push(Element)

Now let us understand the working of push() function with the help of examples.

现在,让我们借助示例来了解push()函数的工作方式。

Example 1:

范例1:

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

# Initializing arrays
Arr1 = [11, 12, 13, 14] 
Arr2 = ["x", "y", "z"] 
Arr3 = ["Includehelp", "C#", "Ruby"]
Arr4 = ["Kiwi","Banana","Orange","Papaya"] 
   
# Invoking push() function with elements to be added 
Arr1push = Arr1.push(15, 16, 17)                 
Arr2push = Arr2.push("k", "l", "m")                
Arr3push = Arr3.push("Python")
Arr4push = Arr4.push("Grapes")   
   
# Printing the array of pushed element 
puts "#{Arr1push}"
puts "#{Arr2push}"
puts "#{Arr3push}"
puts "#{Arr4push}"

Output

输出量

[11, 12, 13, 14, 15, 16, 17]
["x", "y", "z", "k", "l", "m"]
["Includehelp", "C#", "Ruby", "Python"]
["Kiwi", "Banana", "Orange", "Papaya", "Grapes"]

Code logic:

代码逻辑:

We have created four arrays. We are invoking push() function with different arrays which are returning the updated array in the corresponding new array. You can see the reflection of all pushed elements in the new array.

我们创建了四个数组。 我们正在使用具有不同数组的push()函数 ,这些数组将在对应的新数组中返回更新后的数组。 您可以看到新数组中所有推送元素的反射。

If you don't want to create a new array and want to store the new elements in the same Array, then it is also permissible. If you want to push elements in Arr2 then you only have to invoke push function like Arr2.push("k", "l", "m").

如果您不想创建一个新数组并将新元素存储在同一数组中,那么它也是允许的。 如果要在Arr2中推送元素,则只需调用类似Arr2.push(“ k”,“ l”,“ m”)的推送函数。

Now when will print Arr2, all the pushed elements will be reflected in the array Arr2.

现在什么时候打印Arr2 ,所有被压入的元素都将反映在数组Arr2中 。

Example 2:

范例2:

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

# Initializing some arrays of elements 
Arr1 = [100, 201, 301, 401] 
Arr2 = ["KA", "KHA", "GA"] 
Arr3 = ["Meera", "Shayama", "Harish"] 
  
# Initializing some elements  
# which are to be pushed 
num = 501, 601
str = "GHA", "CHA", "PA"
nme = "Radha", "Sabri"
  
# Calling push() function 
A = Arr1.push(num)                 
B = Arr2.push(str)                
C = Arr3.push(nme)   
  
# Printing the array of pushed element 
puts "#{A}"
puts "#{B}"
puts "#{C}"

Output

输出量

[100, 201, 301, 401, [501, 601]]
["KA", "KHA", "GA", ["GHA", "CHA", "PA"]]
["Meera", "Shayama", "Harish", ["Radha", "Sabri"]]

You can observe in the above code that if we invoke push with variables(which have some values), it creates a 2D array. A 2D array is nothing but an array inside an array.

您可以在上面的代码中观察到,如果我们调用带变量的push(具有一些值),它将创建一个2D数组。 2D数组不过是数组内部的数组。

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

ruby 执行函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值