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

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for Hash class. The changes created by this method are permanent or non-temporary. This method works in a way that it will delete all the keys for which the block has been evaluated to be true. If you are not providing any block then an enumerator will be returned.

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 通过此方法创建的更改是永久的或非临时的。 此方法以一种方式删除所有已被评估为真的键。 如果您不提供任何块,则将返回一个枚举器。

Syntax:

句法:

    Hash_object.delete_if{|key,value| block}

Argument(s) required:

所需参数:

This method does not require any argument. However, a block can be passed for the desired result.

此方法不需要任何参数。 但是,可以传递一个块以获得所需的结果。

Example 1:

范例1:

=begin
Ruby program to demonstrate delete_if method
=end	
hsh = Hash.new()
hsh["color"] = "Black"
hsh["age"] = 20
hsh["school"] = "Angels' Academy Haridwar"
hsh["college"] = "Graphic Era University"
puts "Hash delete_if implementation"
puts "#{hsh.delete_if{|key,value| key>="school"}}"
puts "Hash contents are : #{hsh}"

Output

输出量

Hash delete_if implementation
{"color"=>"Black", "age"=>20, "college"=>"Graphic Era University"}
Hash contents are : {"color"=>"Black", "age"=>20, "college"=>"Graphic Era University"}

Explanation:

说明:

In the above code, you can observe that we are removing keys from the hash object based on some condition. The method is returning a hash which is containing all those keys which stood false when the condition was tested on them. The method is creating permanent change on the hash object.

在上面的代码中,您可以观察到我们正在基于某种条件从哈希对象中删除键。 该方法将返回一个散列,其中包含所有在条件上进行过测试时都为假的键。 该方法在哈希对象上创建永久更改。

Example 2:

范例2:

=begin
Ruby program to demonstrate delete_if method
=end	
hsh = Hash.new()
hsh["color"] = "Black"
hsh["age"] = 20
hsh["school"] = "Angels' Academy Haridwar"
hsh["college"] = "Graphic Era University"
puts "Hash delete_if implementation"
puts "#{hsh.delete_if}"
puts "Hash contents are : #{hsh}"

Output

输出量

Hash delete_if implementation
#<Enumerator:0x00005654a524a428>
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}

Explanation:

说明:

In the above code, you can observe that when we are invoking the method without any block, an enumerator has been returned without creating any changes in the actual hash.

在上面的代码中,您可以观察到,当我们在不带任何块的情况下调用该方法时,将返回一个枚举数,而不会在实际哈希中创建任何更改。

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

as_hash ruby

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

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

相关文章

java学习笔记十二——多态

满足多态的基本条件1、要有继承2、要有重写3、父类引用指向子类对象/** 多态例子 */ //定义游戏抽象类abstract class gameObject { String gameName; abstract String getGameName();}//红警游戏class redAlert extends gameObject { String gameName "red Ale…

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 解决方案

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 解决方案 NoClassDefFoundErrorLogFactorySpringHibernate Spring3.1启动时报错&#xff1a;Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogF…

【C++ grammar】引用

1、引用就是另一个变量的别名 2、通过引用所做的读写操作实际上是作用与原变量上 引用方式&#xff1a; int x; int & rxx; or int x, &rxx;在C中&是取地址&#xff0c;在C中&放在一个变量的定义前&#xff0c;那就是引用 注意&#xff1a; 这种引用是错误的…

网络安全 攻击类型_网络攻击的类型| 网络安全

网络安全 攻击类型Nowadays Cyber Security is an essential part of our life. We are store, secure and use data and security is important to prevent to steal data. There are numbers of attacks done by the attackers for ex: Man in middle attacks, Brute force, …

flash安全策略的理解

flash安全策略的理解 2011-06-25 01:48 11人阅读 评论(0) 收藏 举报 一直以来对flash的安全策略是一头雾水&#xff0c;什么安全沙箱&#xff0c;跨域策略文件一堆东西乱七八糟&#xff0c;搞不清楚。不过纠结到现在已经基本上理解了。 flash的安全问题在官方手册上有足够的解…

【C++ grammar】nullptr and Dynamic Memory Allocation (空指针和动态内存分配)

空指针 1.1. 0带来的二义性问题 C03中&#xff0c;空指针使用“0”来表示。0既是一个常量整数&#xff0c;也是一个常量空指针。C语言中&#xff0c;空指针使用(void *)0来表示有时候&#xff0c;用“NULL”来表示空指针(一种可能的实现方式是#define NULL 0) 1.2. C标准化委…

yum update Transaction Check Error

update系统时&#xff0c;发现其中一台server居然提示&#xff1a; Transaction Check Error:file /usr/lib/perl5/5.8.8/CGI.pm from install of perl-5.8.8-32.el5_7.6.x86_64 conflicts with file from package perl-5.8.8-32.el5_6.3.i386file /usr/lib/perl5/5.8.8/Digest…

No module named ‘skimage.metrics‘在Anaconda3中的解决方法

1&#xff0c;进入Anaconda Prompt 2&#xff0c;进行安装&#xff1a; pip install scikit-image 3&#xff0c;若还是报错&#xff0c;尝试进行更新一下 pip install scikit-image --upgrade

array_push_PHP array_push()函数与示例

array_pushPHP array_push()函数 (PHP array_push() function) array_push() function is used to insert/push one or more than one element to the array. array_push()函数用于将一个或多个元素插入/推入数组。 Syntax: 句法&#xff1a; array_push(array, elemement1, …

html5的canvas元素使用,HTML5canvas元素概念及使用方法介绍

一. canvas简介??canvas是html5新增的元素&#xff0c;主要用于图形的绘制。canvas本身没有绘图能力&#xff0c;一切都是通过javascript来完成的。二. canvas使用在html中添加canvas元素&#xff0c;定义canvas元素的高宽在javascript代码中获取canvas元素对象var canvas d…

Delphi XE2 之 FireMonkey 入门(10) - 常用结构 TPoint、TPointF、TSmallPoint、TSize、TRect、TRectF 及相关方法...

它们都是结构, TPointF、TRectF 属新增, 其它也都有升级; 现在都拥有丰富的方法和方便的运算符重载; 且有一组相关的公共函数.这组内容重要的是它们都来自 System.Types 单元, 也就是不仅仅在 FM 中可用. TPoint: Create(); // {运算符重载} Equal; // NotEqual;…

【C++ grammar】数据类型转换、列表初始化

布尔类型 C语言在其标准化过程中引入了bool、true和false关键字&#xff0c;增加了原生数据类型来支持布尔数据。 布尔类型的大小&#xff08;所占的存储空间&#xff09;依赖于具体的编译器实现。也可以用 sizeof运算符得到其占用的空间 Conversion between bool and int 0…

Python对自定义离散点进行指定多项式函数拟合

自定义离散点进行指定多项式函数拟合 用户自己自己输入坐标点&#xff0c;拟合函数可根据用户输入的多项式的最高次方进行自动拟合函数&#xff0c;拟合方法采用最小二乘法进行函数拟合。 (1,2),(2,5),(3,10),(4,17),(5,26),(6,37)(7,50),(8,65),(9,82) 很显然是函数为二次函…

选哪个云计算平台部署自己的网站?

技术发展让我们可以拥有免费的空间啦&#xff01; 现在的云计算平台很多&#xff0c;各大公司都在做这个。 我最近用了国内访问比较顺畅的&#xff0c;新浪SAE&#xff0c;cloudfoundry&#xff0c;openshift。 开始用新浪SAE&#xff0c;因为豆子花光了&#xff0c;就转到clou…

array_fill_PHP array_fill()函数与示例

array_fillPHP array_fill()函数 (PHP array_fill() Function) array_fill() function is used to fill the n elements in an array from given index with the specific value. array_fill()函数用于使用给定索引从给定索引中填充数组中的n个元素。 Syntax: 句法&#xff1a…

阜阳市计算机学校助学金申请书,计算机系贫困生助学金申请书

尊敬的学校领导&#xff1a;您好&#xff01;我叫____来自计算机系____级____专业____班,我是一名喜爱读书、热爱集体并且性格温和的男孩。我出生在一个贫穷而又落后的小村。家中有五口人&#xff0c;父母文化浅薄,在家务农,由于多年的劳累,父母两人身体状况较差,农业收入低微,…

【C++ grammar】C++简化内存模型

1、stack&#xff08;栈&#xff09; 编译器自动分配内存&#xff08;函数内部定义得局部变量、形参&#xff09; 2、堆&#xff08;Heap&#xff09; 一般由程序员分配释放&#xff0c;若程序员不释放&#xff0c;程序结束时可能由OS回收&#xff08;new和delete&#xff09; …

Effect of Diethylene Glycol on the Inkjet Printability of Reactive Dye Solution for Cotton Fabrics.

Effect of Diethylene Glycol on the Inkjet Printability of Reactive Dye Solution for Cotton Fabrics 二乙二醇对棉织物活性染料溶液喷墨印刷性能的影响 Diethylene Glycol 二乙二醇 Inkjet Printability 喷墨印刷性能 Reactive Dye Solution 活性染料溶液 Cotton Fabric…

一些常用的工具

可查看网站服务器使用的架构 http://toolbar.netcraft.com/site_report?urlwww.163.com 可在线制作 icon 图标 http://www.damotou.com/index.php 好的UI设计 http://www.uiimg.com/ 转载于:https://www.cnblogs.com/ouuy/p/3183613.html

Silverlight4.0教程之使用CompositeTransform复合变形特效实现倒影

微软于PDC2009上发布Silverlight 4 Beta版&#xff0c;微软在Silverlight 4版本中处理了约8000个的Silverlight终端用户的请求&#xff0c;加入了一系列另开发人员兴奋的新特性&#xff0c;最突出的主要体现在几个方面&#xff1a; 开发工具增强&#xff1a;Visual Studio 2010…