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

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for 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 destructive methods where changes created by the methods are permanent or non-temporary.

此方法是破坏性方法的示例之一,该方法所产生的更改是永久性的或非临时性的。

Syntax:

句法:

    Hash_object.keep_if
or
Hash_object.keep_if{|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 keep_if method
=end	
hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}
puts "Hash keep_if implementation"
puts "Enter the key you want to keep:"
ky = gets.chomp
puts "Hash after keep_if :#{hash1.keep_if{|key,value| key==ky}}"
puts "Self hash object : #{hash1}"

Output

输出量

Hash keep_if implementation
Enter the key you want to keep:
color
Hash after keep_if :{"color"=>"Black"}
Self hash object : {"color"=>"Black"}

Explanation:

说明:

In the above code, you can observe that we are deleting elements from the hash object with the help of Hash.keep_if 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 destructive methods because it is creating changes in the self hash objects.

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

Example 2:

范例2:

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

Output

输出量

Hash keep_if implementation
Hash after keep_if :#<Enumerator:0x000055f1998a3b90>
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-keep_if-method-with-example.aspx

as_hash ruby

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544123.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

高并发下如何防重?

前言最近测试给我提了一个bug&#xff0c;说我之前提供的一个批量复制商品的接口&#xff0c;产生了重复的商品数据。追查原因之后发现&#xff0c;这个事情没想象中简单&#xff0c;可以说一波多折。1. 需求产品有个需求&#xff1a;用户选择一些品牌&#xff0c;点击确定按钮…

Virtualbox中win7虚拟机中U盘不可用问题的解决

Virtualbox版本是5.0.0&#xff0c;主机运行多是Ubuntu12.04 LTS&#xff0c;虚拟机是Win7 X64。起初Win7正常运行&#xff0c;Virtualbox的增强功能已安装。下面是如何一步一步解决U盘不可用的详细过程。 1. 直接插入U盘&#xff0c;Win7下无任何反映 Virtualbox管理器中&…

面试突击55:delete、drop、truncate有什么区别?

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在 MySQL 中&#xff0c;删除的方法总共有 3 种&#xff1a;delete、truncate、drop&#xff0c;而三者的用法和使用…

python中八进制_在Python中以八进制格式输入数字

python中八进制Syntax to convert octal value to an integer (decimal format), 将八进制值转换为整数(十进制格式)的语法&#xff0c; int(oct_value, 8)Here, 这里&#xff0c; oct_value should contain the valid octal value oct_value应该包含有效的八进制值 8 is the…

Ruby file操作cheatsheet

每次都要查&#xff0c;真是蛋疼&#xff0c;不如一次性总结一下&#xff0c;以后再不记得就来这里找好了。以下代码中需要用到的文件名&#xff1a;filename ‘testfile.txt’ 读取其中的全部内容&#xff1a;File.read(filename)将一个字符串一次性写入这个文件&#xff1a;…

大厂也在用的 6种 数据脱敏方案,别做泄密内鬼

最近连着几天晚上在家总是接到一些奇奇怪怪的电话&#xff0c;“哥&#xff0c;你是 xxx 吧&#xff0c;我们这里是 xxx 高端男士私人会所...”&#xff0c;握草&#xff0c;我先是一愣&#xff0c;然后狠狠的骂了回去。一脸傲娇的转过头&#xff0c;面带微笑稍显谄媚&#xff…

在Python中使用OpenCV裁剪图像

What is Cropping? 什么是播种&#xff1f; Cropping is the removal of unwanted outer areas from a photographic or illustrated image. The process usually consists of the removal of some of the peripheral areas of an image to remove extraneous trash from the…

面渣逆袭:RocketMQ二十三问

1.为什么要使用消息队列呢&#xff1f;消息队列主要有三大用途&#xff0c;我们拿一个电商系统的下单举例&#xff1a;解耦&#xff1a;引入消息队列之前&#xff0c;下单完成之后&#xff0c;需要订单服务去调用库存服务减库存&#xff0c;调用营销服务加营销数据……引入消息…

vue项目打包体积大优化之-productionSourceMap设置

一、productionSourceMap 的作用 productionSourceMap 在构建时生成完整的 SourceMap 文件&#xff0c;默认情况下开启。生产环境中启用 productionSourceMap 有助于开发者调试代码&#xff0c;可以在浏览器的调试工具中查看到源文件中错误的代码位置&#xff0c;而不是编译后…

Java日志性能那些事(转)

在任何系统中&#xff0c;日志都是非常重要的组成部分&#xff0c;它是反映系统运行情况的重要依据&#xff0c;也是排查问题时的必要线索。绝大多数人都认可日志的重要性&#xff0c;但是又有多少人仔细想过该怎么打日志&#xff0c;日志对性能的影响究竟有多大呢&#xff1f;…

如何在Java中使ArrayList只读?

使ArrayList只读 (Making ArrayList Read-Only) Given an ArrayList, and we have to make it Read-Only in Java. 给定一个ArrayList&#xff0c;我们必须使其成为Java只读。 Read-Only: If we make ArrayList as Read-Only i.e. we can only read ArrayList and we cannot p…

33岁程序员的年中总结

作者 | 磊哥来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;人生在不同的阶段会有不同的生活方式和思考问题的角度&#xff0c;这是一件非常有趣的事~ 比如&#xff0c;我在 22 岁会想&…

减治求有重复元素的全排列

求n个元素的全排列的所有解可以用减治法&#xff1a;每次拎出一个数做前缀&#xff0c;对剩下的元素再求全排列&#xff0c;直至只剩一个元素。代码源自《算法分析与设计&#xff08;王晓东&#xff09;》&#xff0c;复杂度O(n&#xff01;) 1 //输出k~m的所有全排列2 void pe…

数据科学中的简单线性回归

简单线性回归 (Simple Linear Regression) A simple regression model could be a linear approximation of a causative relationship between two or additional variables. Regressions models are extremely valuable, as theyre one in every of the foremost common ways…

鹅厂一面,有关 ThreadLocal 的一切

1. 底层结构ThreadLocal 底层有一个默认容量为 16 的数组组成&#xff0c;k 是 ThreadLocal 对象的引用&#xff0c;v 是要放到 TheadLocal 的值public void set(T value) {Thread t Thread.currentThread();ThreadLocalMap map getMap(t);if (map ! null)map.set(this, valu…

再战“超融合”,戴尔、Nutanix绝世好CP

从进入PC领域开始&#xff0c;戴尔一直在扮演颠覆者的角色。戴尔的理想是以开放、标准化的技术和解决方案颠覆传统的封闭的技术和市场&#xff0c;实现与合作伙伴的共赢。在超融合架构逐渐兴起的今天&#xff0c;戴尔依旧希望以变革者的身份&#xff0c;携手超融合架构的先驱Nu…

ruby array_Ruby中带有示例的Array.index()方法

ruby arrayArray.index()方法 (Array.index() Method) In this article, we will study about Array.index() method. You all must be thinking the method must be doing something which is related index of certain element. It is not as simple as it looks. Well, we w…

面试突击58:truncate、delete和drop的6大区别!

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在 MySQL 中&#xff0c;使用 truncate、delete 和 drop 都可以实现表删除&#xff0c;但它们 3 个的使用场景和执行…

scala 去除重复元素_Scala程序从列表中删除重复项

scala 去除重复元素List in Scala is a collection that stores data in the form of a liked-list. The list is an immutable data structure but may contain duplicate elements. And in real life implementation duplicate elements increase the runtime of the program…

智力游戏

【Description】whitecloth 最近迷上了一个你小时候已经玩厌了的游戏&#xff1a;移火柴棒。他现在吵着要你陪他玩&#xff0c;你没有办法&#xff0c;只好写一个程序来完成这个工作了。你被给出了一个火柴拼成的等式&#xff0c;比如说下面这个&#xff1a;&#xff08; 5 7 …