ruby 数组删除部分数组
An array stores multiple values of the same data type. Array is similar to range, but the only difference is that range must represent continuous sequences. Array on the other hand contains collection of data; it doesn't need to be consecutive sequence. The real life examples of arrays are:
数组存储相同数据类型的多个值。 数组类似于range ,但是唯一的区别是range必须表示连续的序列。 另一方面,数组包含数据集合; 它不需要是连续的序列。 数组的实际示例是:
Egg cartons containing eggs
装鸡蛋的鸡蛋盒
Chess board with its pieces etc
棋盘及其碎片等
Representation of Array
数组表示
The numbers 0 to 7 are called Array Index
. They are used to access the array elements.
数字0到7被称为Array Index
。 它们用于访问数组元素。
Ruby:定义数组 (Ruby: Defining an Array)
We define an array by putting square brackets around data. For example,
我们通过在数据周围放置方括号来定义数组。 例如,
numbers = [1,3,5,7,9]
Here numbers
is a collection of 5 numbers(data) of type integers
.
这里的numbers
是5个 integers
类型的数字(数据)的集合。
The element of an array is accessed by using array name followed by index. For example, the first element of numbers
array is accessed by numbers[0]
数组的元素是通过使用数组名和索引来访问的。 例如, numbers
数组的第一个元素可被数字[0]访问
To sum up all the elements of the grades array, following statement is used.
要汇总成绩数组的所有元素,请使用以下语句。
Sum = grades[0] + grades[1] + grades[2] + grades[3]
However, there are more efficient ways of accessing all the elements of an array. We look at those, when we get to our Lesson about Loops. As of now, to access the array elements you specify its index. Sometimes it is called offset.
但是,有更有效的方法可以访问数组的所有元素。 当我们进入有关循环的课程时,我们会看看这些内容。 到目前为止,要访问数组元素,请指定其索引。 有时它称为偏移量 。
Ruby:将序列转换为数组 (Ruby: Turning Sequence into an Array)
The sequences(Range) can be converted into array using to_a
method.
可以使用to_a
方法将sequence(Range)转换为数组。
Ruby:什么是哈希? (Ruby: What is Hash?)
Hash
is another data structure to store the collection of data. It is often called as associative structure, because we store data in key-value
pairs. A classic example of associative structure is Dictionary, where the word is the key and the definition of the word is the value. In hash, to find the value we have to look at the key.
Hash
是另一个用于存储数据集合的数据结构。 它通常被称为关联结构 ,因为我们将数据存储在key-value
对中。 联想结构的一个典型例子是字典 ,其中单词是关键字 ,单词的定义是价值 。 在哈希中,要找到值,我们必须查看键。
Below is an example demonstrating how to define a hash in ruby :
以下是演示如何在ruby中定义哈希的示例:
pincode = {
'Abc' = '123',
'xyz' = '980',
'def' = '456',
}
Ruby:哈希表述 (Ruby: Representation of Hash)
We retrieve the value from the hash
by presenting the key to the hash name like an array subscript.
通过将hash
的键 名呈现给hash
,我们可以从hash
检索值。
To retrieve the number of Praveen : numbers['Praveen']
检索Praveen的数量: numbers['Praveen']
If we try to retrieve a value using a key that doesn't exist, it returns nil
.
如果我们尝试使用不存在的键来检索值,则它将返回nil
。
This way you can ensure that whether the key exists or not. If it returns nil
it represents that the key doesn't exist.
这样,您可以确保密钥是否存在。 如果返回nil
则表示该键不存在。
ruby 数组删除部分数组