ruby 数组删除部分数组_您应该知道的最常见的Ruby数组方法

ruby 数组删除部分数组

通用数组方法 (Common Array Methods)

Ruby Arrays form a core foundation in programming in Ruby, and most languages in fact. It is used so much that it would be beneficial to know and even memorize some of the most commonly used methods for arrays. If you want to know more about Ruby Arrays, we have an article about them.

Ruby Arrays构成了Ruby和事实上大多数语言编程的核心基础。 它的用法如此之多,以至于了解甚至记住一些最常用的数组方法将是有益的。 如果您想了解有关Ruby Arrays的更多信息,请参阅有关它们的文章

For the purpose of this guide, our array will be as follows:

就本指南而言,我们的数组如下:

array = [0, 1, 2, 3, 4]
。长度 (.length)

The .length method tallies the number of elements in your array and returns the count:

.length方法计算数组中元素的数量并返回计数:

array.length
=> 5
。第一 (.first)

The .first method accesses the first element of the array, the element at index 0:

.first方法访问数组的第一个元素,即索引为0的元素:

array.first
=> 0
。持续 (.last)

The .last method accesses the last element of the array:

.last方法访问数组的最后一个元素:

array.last
=> 4
。采取 (.take)

The .take method returns the first n elements of the array:

.take方法返回数组的前n个元素:

array.take(3)
=> [0, 1, 2]
。下降 (.drop)

The .drop method returns the elements after n elements of the array:

.drop方法返回数组中n个元素之后的元素:

array.drop(3)
=> [3, 4]
数组索引 (array index)

You can access a specific element in an array by accessing its index. If the index does not exist in the array, nil will be returned:

您可以通过访问数组中的索引来访问它的特定元素。 如果数组中不存在索引,则返回nil:

array[2]
=> 2

array[5]
=> nil
.pop (.pop)

The .pop method will permantently remove the last element of an array:

.pop方法将永久删除数组的最后一个元素:

array.pop
=> [0, 1, 2, 3]
。转移 (.shift)

The .shift method will permantently remove the first element of an array and return this element:

.shift方法将永久删除数组的第一个元素并返回此元素:

array.shift
=> 0  
array
=> [1, 2, 3, 4]
。推 (.push)

The .push method will allow you to add an element to the end of an array:

.push方法将允许您将元素添加到数组的末尾:

array.push(99)
=> [0, 1, 2, 3, 4, 99]
.unshift (.unshift)

The .unshift method will allow you to add an element to the beginning of an array:

.unshift方法将允许您将元素添加到数组的开头:

array = [2, 3]
array.unshift(1)
=> [1, 2, 3]
。删除 (.delete)

The .delete method removes a specified element from an array permanently:

.delete方法从数组中永久删除指定的元素:

array.delete(1)
=> [0, 2, 3, 4]
.delete_at (.delete_at)

The .delete_at method allows you to permanently remove an element of an array at a specified index:

.delete_at方法允许您永久删除指定索引处的数组元素:

array.delete_at(0)
=> [1, 2, 3, 4]
。逆转 (.reverse)

The .reverse method reverses the array but does not mutate it (the original array stays as is):

.reverse方法反转数组,但不对其进行突变(原始数组保持原样):

array.reverse
=> [4, 3, 2, 1, 0]
。选择 (.select)

The .select method iterates over an array and returns a new array that includes any items that return true to the expression provided.

.select方法遍历数组,并返回一个新数组,该数组包含所有对提供的表达式返回true的项。

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array.select { |number| number > 4 }
=> [5, 6, 7, 8, 9, 10]
array
=> [5, 6, 7, 8, 9, 10]
。包括? (.include?)

The include? method checks to see if the argument given is included in the array:

包括吗? 方法检查给定的参数是否包含在数组中:

array = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
array.include?(3)
=> true

#### .flatten
The flatten method can be used to take an array that contains nested arrays and create a one-dimensional array:

``` ruby
array = [1, 2, [3, 4, 5], [6, 7]]
array.flatten
=> [1, 2, 3, 4, 5, 6, 7]
。加入 (.join)

The .join method returns a string of all the elements of the array separated by a separator parameter. If the separator parameter is nil, the method uses an empty string as a separator between strings.

.join方法返回由分隔符参数分隔的数组所有元素的字符串。 如果Separator参数为nil,则该方法使用一个空字符串作为字符串之间的分隔符。

array.join
=> "1234"
array.join("*")
=> "1*2*3*4"
。每 (.each)

The .each method iterates over each element of the array, allowing you to perform actions on them.

.each方法遍历数组的每个元素,使您可以对它们执行操作。

array.each do |element|
  puts element
end
=> 
0
1
2
3
4
。地图 (.map)

The .map method is the same as the .collect method. The .map and .collect methods iterate over each element of the array, allowing you to perform actions on them. The .map and .collect methods differ from the .each method in that they return an array containing the transformed elements.

.map方法与.collect方法相同。 .map和.collect方法遍历数组的每个元素,使您可以对它们执行操作。 .map和.collect方法与.each方法的不同之处在于,它们返回包含转换后的元素的数组。

array.map { |element| element * 2 }
  puts element
end
=> 
0
2
4
6
8
.uniq (.uniq)

The .uniq method takes in an array containing duplicate elements, and returns a copy of the array containing only unique elements—any duplicate elements are removed from the array.

.uniq方法接收包含重复元素的数组,并返回仅包含唯一元素的数组的副本-所有重复元素都将从数组中删除。

array = [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8]
array.uniq
=> [1, 2, 3, 4, 5, 6, 7, 8]
.concat (.concat)

The .concat method appends the elements from an array to the original array. The .concat method can take in multiple arrays as an argument, which will in turn append multiple arrays to the original array.

.concat方法将元素从数组追加到原始数组。 .concat方法可以将多个数组作为参数,然后将多个数组追加到原始数组。

array = [0, 1, 2, 3, 4]
array.concat([5, 6, 7], [8, 9, 10])
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

更多信息 (More Information)

翻译自: https://www.freecodecamp.org/news/common-array-methods-in-ruby/

ruby 数组删除部分数组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值