ruby each_在Ruby中使用Each方法

ruby each

Every array and hash in Ruby is an object, and every object of these types has a set of built-in methods. Programmers new to Ruby can learn about how to use the each method with an array and a hash by following the simple examples presented here.

Ruby中的每个数组和哈希都是一个对象,并且这些类型的每个对象都有一组内置方法。 Ruby新手可以通过遵循此处介绍的简单示例,学习如何将每个方法与数组和哈希一起使用。

在Ruby中将每个方法与数组对象一起使用 ( Using the Each Method With an Array Object in Ruby )

First, create an array object by assigning the array to "stooges."

首先,通过将数组分配给“ stooges”来创建数组对象。

 >> stooges = ['Larry', 'Curly', 'Moe'] 

Next, call the each method and create a small block of code to process the results.

接下来,调用each方法并创建一小段代码来处理结果。

 >> stooges.each { |stooge| print stooge + "\n" } 

This code produces the following output:

此代码产生以下输出:

 Larry
 Curly
 Moe 

The each method takes two arguments—an element and a block. The element, contained within the pipes, is similar to a placeholder. Whatever you put inside the pipes is used in the block to represent each element of the array in turn. The block is the line of code that is executed on each of the array items and is handed the element to process.

每个方法都带有两个参数 -一个元素和一个块。 包含在管道中的元素类似于占位符。 无论您放置在管道中的任何内容,都将在该块中依次用来表示数组的每个元素。 块是在每个数组项上执行的代码行,将其交给元素进行处理。

You can easily extend the code block to multiple lines by using do to define a larger block:

您可以使用do定义一个更大的代码块,轻松地将代码块扩展到多行:

 >> stuff.each do |thing|
 print thing
 print "\n"
 end 

This is the same as the first example, except that the block is defined as everything after the element (in pipes) and before the end statement.

除了将块定义为元素之后(在管道中)和end语句之前的所有内容之外,这与第一个示例相同。

将每个方法与哈希对象一起使用 ( Using the Each Method With a Hash Object )

Just like the array object, the hash object has an each method that can be used to apply a block of code on each item in the hash. First, create a simple hash object that contains some contact information:

就像数组对象一样哈希对象具有each方法,该方法可用于将代码块应用于哈希中的每个项目。 首先,创建一个简单的哈希对象,其中包含一些联系信息:

 >> contact_info = { 'name' => 'Bob', 'phone' => '111-111-1111' } 

Then, call the each method and create a single line block of code to process and print the results.

然后,调用每个方法并创建一行代码以处理和打印结果。

 >> contact_info.each { |key, value| print key + ' = ' + value + "\n" } 

This produces the following output:

这将产生以下输出:

 name = Bob
 phone = 111-111-1111 

This works exactly like the each method for an array object with one crucial difference. For a hash, you create two elements—one for the hash key and one for the value. Like the array, these elements are placeholders that are used to pass each key/value pair into the code block as Ruby loops through the hash.

这与数组对象的每种方法完全一样,但有一个关键的区别。 对于哈希,您将创建两个元素-一个用于哈希键,一个用于值。 像数组一样,这些元素是占位符,用于在Ruby循环遍历哈希时将每个键/值对传递到代码块中。

You can easily extend the code block to multiple lines by using do to define a larger block:

您可以使用do定义一个更大的代码块,轻松地将代码块扩展到多行:

 >> contact_info.each do |key, value|
 print print key + ' = ' + value
 print "\n"
end 

This is the same as the first hash example, except that the block is defined as everything after the elements (in pipes) and before the end statement.

除了将块定义为元素之后(在管道中)和end语句之前的所有内容之外,这与第一个哈希示例相同。

翻译自: https://www.thoughtco.com/using-each-beginning-ruby-control-structures-2641202

ruby each

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值