ruby hash方法

哈希选择方法 (Hash.select Method)

In this article, we will study about Hash.select 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.select方法 。 可以借助其名称来预测此方法的工作,但是它并不像看起来那样简单。 好了,我们将在其余内容中借助其语法和程序代码来理解此方法。

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 it removes every key-value pair from the hash object for which the block has evaluated to be false. If you are not providing any block, then this method will return an enumerator.

此方法是在ruby库中定义的公共实例方法,尤其是针对Hash类。 此方法的工作方式是,从该块已评估为false的哈希对象中删除每个键值对。 如果不提供任何块,则此方法将返回一个枚举器。

This method is one of the examples of non-destructive methods where changes created by the methods are non-permanent or temporary.

此方法是非破坏性方法的示例之一,其中,由这些方法创建的更改是非永久性或临时性的。

Syntax:

句法:

    Hash_object.select
or
Hash_object.select{|key,value| block}

Argument(s) required:

所需参数:

This method does not require any argument. You will need to pass a block with the method for its better implementation.

此方法不需要任何参数。 您将需要将该方法传递给一个块,以实现更好的实现。

Example 1:

范例1:

=begin
Ruby program to demonstrate select method
=end
hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}
puts "Hash select implementation"
puts "Enter the key you want to keep:"
ky = gets.chomp
puts "Hash after select :#{hash1.select{|key,value| key==ky}}"
puts "Self hash object : #{hash1}"

Output

输出量

Hash select implementation
Enter the key you want to keep:
color
Hash after select :{"color"=>"Black"}
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

说明:

In the above code, you can observe that we are deleting elements from the hash object with the help of the Hash.select() method. You can see that the method is deleting all the elements for which the method has returned false. This method is one of the examples of non-destructive methods because it is not creating changes in the self hash object.

在上面的代码中,您可以观察到我们借助于Hash.select()方法从哈希对象中删除元素。 您可以看到该方法正在删除该方法返回false的所有元素。 此方法是非破坏性方法的示例之一,因为它没有在自哈希对象中创建更改。

Example 2:

范例2:

=begin
Ruby program to demonstrate select method
=end
hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}
puts "Hash select implementation"
puts "Hash after select :#{hash1.select}"
puts "Self hash object : #{hash1}"

Output

输出量

Hash select implementation
Hash after select :#<Enumerator:0x000055fd5496c880>
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

说明:

In the above code, you can observe that this method returns an enumerator when called without providing any block at the time of invocation.

在上面的代码中,您可以观察到该方法在调用时返回一个枚举器,而在调用时没有提供任何块。

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

ruby hash方法

ruby hash方法_Ruby中带有示例的Hash.select方法相关推荐

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

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

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

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

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

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

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

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

  5. ruby hash方法_Ruby中带有示例的Hash.default(key = nil)方法

    ruby hash方法 Hash.default(key = nil)方法 (Hash.default(key=nil) Method) In this article, we will study ...

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

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

  7. ruby hash方法_Ruby中带有示例的Hash.key?(obj)方法

    ruby hash方法 Hash.key?(obj)方法 (Hash.key?(obj) Method) In this article, we will study about Hash.key?( ...

  8. ruby hash方法_Ruby中带有示例的Hash.rassoc(obj)方法

    ruby hash方法 Hash.rassoc(obj)方法 (Hash.rassoc(obj) Method) In this article, we will study about Hash.r ...

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

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

最新文章

  1. 【ACM】杭电OJ 2037
  2. SQL text字段的替换处理(无法批量更新,写法记录)
  3. Oracle的UNION函数
  4. 2021.01.30 Visual QA论文阅读
  5. php面向对象之单表操作类
  6. 前端学习(95):ps基本操作与图片格式
  7. 数据管理的3种方法,看完后感叹:数字化转型、数据中台真不难
  8. SSH框架调用scrapy爬虫
  9. 云服务器ecs什么意思
  10. 信息科学技术与创新之“信息纽带”
  11. 5年后端WEB开发者的开机必备软件
  12. 电路设计中晶体和晶振的区别
  13. adlds文件服务器,WinServer2008之ADLDS轻型目录服务解析
  14. C# Excel操作之读,写,追加
  15. ESXi通过命令行创建并直通RDM磁盘
  16. XP 系统开机自动登录
  17. Android RTL布局适配
  18. Java中super详解
  19. 人工智能除了下围棋还能干啥?
  20. 学习IT的一点经验分享

热门文章

  1. 教你玩转CSS 组合选择符
  2. 有什么好一点的方法读jdk源码吗?
  3. springboot中得注解_Spring以及SpringBoot中的常用的注解小结
  4. java两个和三个_Java语言基础(day_03)
  5. vue2.0中的:is和is的区别
  6. 简单却实用的的例子:Jquery获取 radio 选中后的文字
  7. 【javascript】DOM操作方法(3)——document节点属性
  8. eclipse 下使用git clone
  9. java基础----数字签名算法的介绍
  10. [PALAPALA] 无题 - 外来的和尚会念经