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 :
在本课程中,我们将学习有关数组的更多信息,以及在数组中add
和remove
元素的更多方法。 打开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")


Now, you can see that there are three items in array named sample
.
现在,您可以看到数组中有三个名为sample
项目。

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 operation
弹出操作

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,
记得,
push
- to insert item or element into an array.push
将项目或元素插入数组。pop
- to remove an item or an element from an array.pop
从数组中删除项目或元素。
ruby中的bind方法