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
=end
hsh={"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 implementation
name is Zorawar
class is ukg
school is AASSC
place 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
=end
hsh={"name"=>"Zorawar","class"=>"ukg","school"=>"AASSC","place"=>"Haridwar"}
puts "Hash each_pair implementation"
str = hsh.each_pair
puts str

Output

输出量

Hash each_pair implementation
#<Enumerator:0x0000560701eb6820>

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.

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

翻译自: https://www.includehelp.com/ruby/hash-each_pair-method-with-example.aspx

as_hash ruby

as_hash ruby_Ruby中带有示例的Hash.each_pair方法相关推荐

  1. as_hash ruby_Ruby中带有示例的Hash.keep_if方法

    as_hash ruby Hash.keep_if方法 (Hash.keep_if Method) In this article, we will study about Hash.keep_if ...

  2. as_hash ruby_Ruby中带有示例的Hash.delete_if方法

    as_hash ruby Hash.delete_if方法 (Hash.delete_if Method) In this article, we will study about Hash.dele ...

  3. ruby hash方法_Ruby中带有示例的Hash.invert方法

    ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...

  4. ruby hash方法_Ruby中带有示例的Hash.select方法

    ruby hash方法 哈希选择方法 (Hash.select Method) In this article, we will study about Hash.select Method. The ...

  5. ruby hash方法_Ruby中带有示例的Hash.flatten方法

    ruby hash方法 哈希平化方法 (Hash.flatten Method) In this article, we will study about Hash.flatten Method. T ...

  6. ruby hash方法_Ruby中带有示例的Hash.rehash方法

    ruby hash方法 Hash.rehash方法 (Hash.rehash Method) In this article, we will study about Hash.rehash Meth ...

  7. ruby hash方法_Ruby中带有示例的Hash.values方法

    ruby hash方法 哈希值方法 (Hash.values Method) In this article, we will study about Hash.values Method. The ...

  8. ruby hash方法_Ruby中带有示例的Hash.length方法

    ruby hash方法 哈希长度方法 (Hash.length Method) In this article, we will study about Hash.length Method. The ...

  9. ruby hash方法_Ruby中带有示例的Hash.keys方法

    ruby hash方法 哈希键方法 (Hash.keys Method) In this article, we will study about Hash.keys Method. The work ...

最新文章

  1. Spark的transformation和action算子简介
  2. 页面滑动至某处,固定导航。
  3. MySQL:创建、修改和删除表
  4. Android中实现调用摄像头拍照并显示在ImageView中
  5. windows 2008创建群集“xxx”时出错。由于超时时间已过,该操作返回
  6. 删除bin后,Eclipse重新编译项目
  7. 指向函数的指针数组(C++)
  8. 面试题50. 第一个只出现一次的字符
  9. 程序员版《On Call 24 小时》
  10. ASP.NET MVC中ApiController与Controller的区别
  11. UITableViewCell高度自适应变化
  12. html css没有反应,只有CSS和HTML的标签没有正确响应
  13. 基于 TI 方案 CC2540 低功耗无线蓝牙模块详解
  14. 集成电路设计-读书笔记(一)
  15. 如何在官网下载hbase
  16. android模拟机新闻APP,Exagear ET(Exagear模拟器)
  17. 公众号获取token失败_如何利用公众号获取电影引流?如何利用电影网站嫁接公众号?...
  18. C++核心准则T.61:不要过度参数化成员(SCARY)
  19. 自动驾驶两大路线对决,渐进式玩家为何更容易得人心?
  20. 【xlwings api语言参考】Worksheet.Cells 属性

热门文章

  1. (十)Linux之等待队列
  2. 恒流电路的分析(一)
  3. linux boost教程,Linux上安装使用Boost入门指导
  4. 量子计算机怎么编程,量子计算机编程(一)——QPU编程
  5. python连接mysql代码_Python3连接MySQL(pymysql)转账代码
  6. python mysql 正则表达式,MySQL之正则表达式(REGEXP)
  7. HTML5中本地储存概念是什么,什么优点 ,与cookie有什么区别?
  8. 苏宁 11.11:仓库内多 AGV 协作的全局路径规划算法研究
  9. 序(不知道是什么时候的模拟题)
  10. Android O 获取APK文件权限 Demo案例