博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
as_hash ruby_Ruby中带有示例的Hash.each_pair方法
阅读量:2530 次
发布时间:2019-05-11

本文共 2705 字,大约阅读时间需要 9 分钟。

as_hash ruby

Hash.each_pair方法 (Hash.each_pair Method)

In this article, we will study about Hash.each_pair Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method with the help of its syntax and program code in the rest of the content.

在本文中,我们将研究Hash.each_pair方法 。 可以借助其名称来预测此方法的工作,但是它并不像看起来那样简单。 好了,我们将在其余内容中借助其语法和程序代码来理解此方法。

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for the Hash class. This method works in a way that invokes the block at least once for every individual key present in the hash object. The key-value pairs present in the hash object are passed as parameters. If you are not providing any block then you should expect an enumerator as the returned value from the each_pair method.

此方法是在ruby库中定义的公共实例方法,尤其是针对Hash类。 此方法的工作方式是,针对哈希对象中存在的每个单个键至少调用一次该块。 哈希对象中存在的键值对作为参数传递。 如果不提供任何块,则应期望枚举数作为each_pair方法的返回值。

Syntax:

句法:

Hash_object.each_pair{|key,value| block}

Argument(s) required:

所需参数:

This method does not accept any arguments. However, you can pass a block along with this method.

此方法不接受任何参数。 但是,您可以通过此方法传递一个块。

Example 1:

范例1:

=begin  Ruby program to demonstrate each_pair method=endhsh={
"name"=>"Zorawar","class"=>"ukg","school"=>"AASSC","place"=>"Haridwar"}puts "Hash each_pair implementation"str = hsh.each_pair{
|key,value| puts "#{
key} is #{
value}"}puts str

Output

输出量

Hash each_pair implementationname is Zorawarclass is ukgschool is AASSCplace is Haridwar{"name"=>"Zorawar", "class"=>"ukg", "school"=>"AASSC", "place"=>"Haridwar"}

Explanation:

说明:

In the above code, you may observe that we are printing every key-value pair from the hash object with the help of the Hash.each_pair method. The method is invoking a block, which is taking the key value as the argument from the hash object. This method is traversing through the hash object, processing them and giving us the desired output or you can say that letting us know about the value stored in a particular key.

在上面的代码中,您可能会看到,借助于Hash.each_pair方法 ,我们正在从哈希对象中打印每个键值对。 该方法正在调用一个块,该块将键值作为哈希对象的参数。 该方法遍历哈希对象,对其进行处理并为我们提供所需的输出,或者您可以说让我们知道存储在特定键中的值。

Example 2:

范例2:

=begin  Ruby program to demonstrate each_pair method=endhsh={
"name"=>"Zorawar","class"=>"ukg","school"=>"AASSC","place"=>"Haridwar"}puts "Hash each_pair implementation"str = hsh.each_pairputs str

Output

输出量

Hash each_pair implementation#

Explanation:

说明:

In the above code, you can observe that when we are invoking the method without the block then we are getting an enumerator returned from the method.

在上面的代码中,您可以观察到,当我们在不使用该块的情况下调用该方法时,就会得到该方法返回的枚举数。

翻译自:

as_hash ruby

转载地址:http://zxtzd.baihongyu.com/

你可能感兴趣的文章
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
iOS语言中的KVO机制
查看>>