ruby中的bind方法_Ruby中的Push和Pop方法

ruby中的bind方法

In this lesson, we are going to learn more about arrays, more ways to add and remove element to and from arrays. Open Interactive ruby shell and type in :

在本课程中,我们将学习有关数组的更多信息,以及在数组中addremove元素的更多方法。 打开Interactive ruby​​ shell并输入:

sample = [ ]

This creates an empty array with no element(s) in it. Now, we are going to add elements to this empty array named sample. You can push the element into an array. You can do that in couple of ways.

这将创建一个空数组,其中没有任何元素。 现在,我们将元素添加到名为sample的空数组中。 您可以将元素推入数组。 您可以通过几种方式来做到这一点。

sample << 'first item'

This statement pushes the string 'first item' into array.

该语句将字符串“ first item”推入数组。

Ruby:推送操作 (Ruby: Push operation)

Another way to add or to push item into an array is using push() method.

将项目添加或推入数组的另一种方法是使用push()方法。

sample.push("second item")
Push function in Ruby
Push function in Ruby

Now, you can see that there are three items in array named sample.

现在,您可以看到数组中有三个名为sample项目。

Push example in Ruby

Ruby:弹出操作 (Ruby: Pop operation)

To remove the element from an array, we use pop() method.

要从数组中删除元素,我们使用pop()方法。

sample.pop

For removing the element, you don't need to specify the parameters. It removes the recently inserted item, no matter what it is.

要删除该元素,无需指定参数。 无论它是什么,它都会删除最近插入的项目。

Pop function in Ruby

Pop operation

弹出操作

Pop function in Ruby

As you can see, the pop operation removed the recently inserted item hakuna matata and then second item. Now, the array contains only one item. These operations allow arrays to be used as STACK data structure.

如您所见, pop操作删除了最近插入的项目hakuna matata ,然后删除了第二个项目 。 现在,该数组仅包含一项。 这些操作允许将数组用作STACK数据结构。

Remember,

记得,

  1. push - to insert item or element into an array.

    push将项目或元素插入数组。

  2. pop - to remove an item or an element from an array.

    pop从数组中删除项目或元素。

翻译自: https://www.studytonight.com/ruby/push-and-pop-in-ruby

ruby中的bind方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值