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

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

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.chompputs "Hash after select :#{hash1.select{
|key,value| key==ky}}"puts "Self hash object : #{hash1}"

Output

输出量

Hash select implementationEnter the key you want to keep: colorHash 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 implementationHash after select :#
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.

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

翻译自:

ruby hash方法

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

你可能感兴趣的文章
hdu 1005 根据递推公式构造矩阵 ( 矩阵快速幂)
查看>>
安装php扩展
查看>>
mysql统计某一个数据库中有几张表
查看>>
百度移动搜索主要有如下几类结果构成
查看>>
梦断代码最后4章读后感
查看>>
python学习---字符串
查看>>
Python爬虫面试题170道:2019版【1】
查看>>
JavaBean规范
查看>>
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
简单的栈和队列
查看>>
接口测试用例
查看>>
面试:用 Java 实现一个 Singleton 模式
查看>>
Sybase IQ导出文件的几种方式
查看>>
案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变...
查看>>
点语法
查看>>
IO之Socket网络编程
查看>>
PCRE demo【转】
查看>>
矩阵的SVD分解
查看>>