博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
as_hash ruby_Hash.default_proc = obj方法(带Ruby中的示例)
阅读量:2529 次
发布时间:2019-05-11

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

as_hash ruby

Hash.default_proc = obj方法 (Hash.default_proc=obj Method)

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

Method description:

方法说明:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method is used to set the default value and that value will be returned when the key is not found while key lookup. By this method, you can set procs as the default value for any hash object. If you don't set a default value then nil will be returned when the key is not found or key is not a part of the hash instance.

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 此方法用于设置默认值,并且在查找密钥时未找到密钥时将返回该值。 通过此方法,您可以将procs设置为任何哈希对象的默认值 。 如果未设置默认值,则在找不到密钥或密钥不是哈希实例的一部分时将返回nil。

Syntax:

句法:

Hash_object.default_proc

Argument(s) required:

所需参数:

This method does not require any argument. This method is used to assign value.

此方法不需要任何参数。 此方法用于分配值。

Example 1:

范例1:

=begin  Ruby program to demonstrate default_proc method=end	hsh = Hash.new()hsh["color"] = "Black"hsh["age"] = 20hsh["school"] = "Angels' Academy Haridwar"hsh["college"] = "Graphic Era University"puts "Hash default_proc implementation"hsh.default_proc = proc do|hash,key|	hsh[key]=key+keyendputs "Hash contents are : #{hsh}"puts "Enter the key you want to find:"ky = gets.chompputs "The value of #{ky} is #{hsh[ky]}"

Output

输出量

Hash default_proc implementationHash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}Enter the key you want to find: residenceThe value of residence is residenceresidence

Explanation:

说明:

In the above code, you can observe that we are setting the proc as the default value of hash with the help of the default_proc method. You can see that when the user has entered a key that is not available in the hash, then the value returned from the proc has been printed.

在上面的代码中,您可以观察到,借助于default_proc方法 ,我们将proc设置为hash的默认值。 您可以看到,当用户输入了哈希中不可用的键时,从proc返回的值就已经打印了。

Example 2:

范例2:

=begin  Ruby program to demonstrate setting proc with default method.=end	hsh = Hash.new()hsh["color"] = "Black"hsh["age"] = 20hsh["school"] = "Angels' Academy Haridwar"hsh["college"] = "Graphic Era University"puts "Hash default implementation"hsh.default = proc do|hash,key|hash = key+keyendputs "Hash contents are : #{hsh}"puts "Enter the key you want to find:"ky = gets.chompputs "The value of #{ky} is #{hsh[ky]}"

Output

输出量

Hash default implementationHash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}Enter the key you want to find: animalThe value of animal is #

Explanation:

说明:

In the above code, you can observe that we are trying to set a proc as the default value of the hash object. You can also observe that we are not getting the desired result because you can never set a proc as the default value of any hash with the help of this method. For completing the task, you will always need the default_proc method.

在上面的代码中,您可以观察到我们正在尝试将proc设置为哈希对象的默认值。 您还可以观察到我们没有得到理想的结果,因为您永远无法借助此方法将proc设置为任何哈希的默认值。 为了完成任务,您将始终需要default_proc方法

翻译自:

as_hash ruby

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

你可能感兴趣的文章
【转】how can i build fast
查看>>
接口测试-jmeter
查看>>
js便签笔记(5)——Dean Edwards大牛的跨浏览器AddEvent()设计(不知道是不是jQuery事件系统的原型)...
查看>>
重构wangEditor(web富文本编辑器),欢迎指正!
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
UIScrollerView ,UIPageControl混搭使用,添加定时器,无限循环滚动
查看>>
图论知识,博客
查看>>
微信企业号开发之-如何获取secret 序列号
查看>>
2015年最新Android基础入门教程目录(完结版)
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>
原生和jQuery的ajax用法
查看>>
iOS开发播放文本
查看>>
20145202马超《java》实验5
查看>>
JQuery 事件
查看>>
main(argc,argv[])
查看>>
源码:Java集合源码之:哈希表(二)
查看>>
【HDU 3709】 Balanced Number (数位DP)
查看>>