ruby 数组删除部分数组_Ruby中的数组

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

    棋盘及其碎片等

Example of Array

Representation of Array

数组表示

The numbers 0 to 7 are called Array Index. They are used to access the array elements.

数字07被称为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.

这里的numbers5个 integers类型的数字(数据)的集合。

Array Example in Ruby

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]访问

Accessing Array Elements in Ruby

To sum up all the elements of the grades array, following statement is used.

要汇总成绩数组的所有元素,请使用以下语句。

Sum = grades[0] + grades[1] + grades[2] + grades[3]
Sum of ELements of Array in Ruby

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)转换为数组。

Turning Range into Array in Ruby

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',
	}	

Hash in Ruby
Representation of Hash in Ruby

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

Accessing Hash elements in Ruby example

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则表示该键不存在。

翻译自: https://www.studytonight.com/ruby/arrays-in-ruby

ruby 数组删除部分数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值