Ruby 简明教程

Ruby - Arrays

Ruby 数组是有序、整数索引的任何对象的集合。数组中的每个元素都与一个索引关联并通过索引引用。

Ruby arrays are ordered, integer-indexed collections of any object. Each element in an array is associated with and referred to by an index.

数组索引从 0 开始,就像在 C 或 Java 中一样。负索引假定相对于数组的末尾——也就是说,索引 -1 表示数组的最后一个元素,-2 是数组中的倒数第二个元素,依此类推。

Array indexing starts at 0, as in C or Java. A negative index is assumed relative to the end of the array --- that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.

Ruby 数组可以包含对象,例如字符串、整数、Fixnum、哈希、符号,甚至是其他数组对象。Ruby 数组不像其他语言中的数组那样严格。在向 Ruby 数组中添加元素时,它们会自动增长。

Ruby arrays can hold objects such as String, Integer, Fixnum, Hash, Symbol, even other Array objects. Ruby arrays are not as rigid as arrays in other languages. Ruby arrays grow automatically while adding elements to them.

Creating Arrays

创建或初始化数组有很多方法。一种方法是使用 new 类方法:

There are many ways to create or initialize an array. One way is with the new class method −

names = Array.new

您能在创建数组时设置数组的大小:

You can set the size of an array at the time of creating array −

names = Array.new(20)

数组名称现在的尺寸或长度是 20 个元素。您可以使用 size 或 length 方法返回数组的大小:

The array names now has a size or length of 20 elements. You can return the size of an array with either the size or length methods −

#!/usr/bin/ruby

names = Array.new(20)
puts names.size  # This returns 20
puts names.length # This also returns 20

这会产生以下结果 −

This will produce the following result −

20
20

您可以按照如下方式给数组中的每个元素赋一个值:

You can assign a value to each element in the array as follows −

#!/usr/bin/ruby

names = Array.new(4, "mac")
puts "#{names}"

这会产生以下结果 −

This will produce the following result −

["mac", "mac", "mac", "mac"]

您还可以在 new 中使用一个块,为每个元素填充块求值的内容:

You can also use a block with new, populating each element with what the block evaluates to −

#!/usr/bin/ruby

nums = Array.new(10) { |e| e = e * 2 }
puts "#{nums}"

这会产生以下结果 −

This will produce the following result −

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Array 还有另一种方法 []。它的工作原理如下:

There is another method of Array, []. It works like this −

nums = Array.[](1, 2, 3, 4,5)

数组创建的另一种形式如下:

One more form of array creation is as follows −

nums = Array[1, 2, 3, 4,5]

核心 Ruby 中提供的 Kernel 模块具有一个 Array 方法,该方法只接受一个参数。在此,该方法将一个范围作为参数来创建一个由数字组成的数组——

The Kernel module available in core Ruby has an Array method, which only accepts a single argument. Here, the method takes a range as an argument to create an array of digits −

#!/usr/bin/ruby

digits = Array(0..9)
puts "#{digits}"

这会产生以下结果 −

This will produce the following result −

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Array Built-in Methods

我们需要一个 Array 对象的实例来调用 Array 方法。如我们所见,以下是创建 Array 对象实例的方法——

We need to have an instance of Array object to call an Array method. As we have seen, following is the way to create an instance of Array object −

Array.[](...) [or] Array[...] [or] [...]

这将返回一个由给定对象填充的新数组。现在,使用创建的对象,我们可以调用任何可用的实例方法。例如——

This will return a new array populated with the given objects. Now, using the created object, we can call any available instance methods. For example −

#!/usr/bin/ruby

digits = Array(0..9)
num = digits.at(6)
puts "#{num}"

这会产生以下结果 −

This will produce the following result −

6

Array pack Directives

Example

尝试以下示例来打包各种数据。

Try the following example to pack various data.

a = [ "a", "b", "c" ]
n = [ 65, 66, 67 ]
puts a.pack("A3A3A3")   #=> "a  b  c  "
puts a.pack("a3a3a3")   #=> "a\000\000b\000\000c\000\000"
puts n.pack("ccc")      #=> "ABC"

这会产生以下结果 −

This will produce the following result −

a  b  c
abc
ABC