ruby hash方法

Hash.default(key = nil)方法 (Hash.default(key=nil) Method)

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

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 returns the default value but now you will be thinking when this method will return the default value. This method will return the default value when the user is trying to access the value of key or simply key which is not available in the hash or you can say that which is not a part of the hash object. The method will print the default value instead of returning 'nil'.

此方法是在ruby库中定义的公共实例方法,特别是针对Hash类。 此方法以返回默认值的方式工作,但是现在您将考虑此方法何时将返回默认值。 当用户尝试访问哈希值中不可用的key值或key值时,此方法将返回默认值,或者您可以说那不是哈希对象的一部分。 该方法将打印默认值,而不是返回'nil'

Syntax:

句法:

    Hash_object.default
or
Hash_object.default(object)

Argument(s) required:

所需参数:

Passing value in this method is optional. If you are passing a value then that value can be an object of any class.

在此方法中传递值是可选的。 如果要传递值,则该值可以是任何类的对象。

Example 1:

范例1:

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

Output

输出量

Hash default implementation
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
default value : Not available

Explanation:

说明:

In the above code, you can observe that we have set a default value with the help of the new method. We are accessing that default value with the help of the default method. The default value is "Not available" and this value will be returned whenever the key is not found in the hash object.

在上面的代码中,您可以看到我们已经在new方法的帮助下设置了默认值。 我们正在借助默认方法访问该默认值。 默认值为“不可用” ,只要在哈希对象中找不到密钥,就会返回此值。

Example 2:

范例2:

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

Output

输出量

Hash default implementation
Hash contents are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
default value : Hello : 2

Explanation:

说明:

In the above code, you can observe that we have set a default value with the help of the new method by passing a block into it. We are accessing that default value with the help of the default method. The default value is "Hello: key" and this value will be returned whenever the key is not found in the hash object. We are passing the key along with the method and that key will be returned along with the value when the key is missing.

在上面的代码中,您可以观察到,借助于将新的块传递给新方法,我们已经设置了默认值。 我们正在借助默认方法访问该默认值。 默认值为“ Hello:key” ,只要在哈希对象中未找到该键,就会返回该值。 我们将键与方法一起传递,当键丢失时,键将与值一起返回。

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

ruby hash方法

ruby hash方法_Ruby中带有示例的Hash.default(key = nil)方法相关推荐

  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.select方法

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

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

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

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

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

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

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

  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. Restore Volume 操作 - 每天5分钟玩转 OpenStack(60)
  2. pandas使用groupby函数计算dataframe数据中每个分组的N个数值的滚动计数个数(rolling count)、例如,计算某公司的多个店铺每N天(5天)的滚动销售额计数个数
  3. codevs 1002 搭桥
  4. 获取jar中的资源文件途径
  5. Oracle 基础教程
  6. 程序人生:女程序员的求职奋斗史
  7. ubuntu 下可以尝试还不错的屏幕截图工具: flameshot
  8. npm是如何处理依赖关系的
  9. 去年我国科技研发投入1.76万亿元,与美国差距逐年缩小
  10. linux下ftp二进制传输,FTP的两种传输模式:BINARY和ASCII
  11. CCSK认证是什么?云计算安全知识认证含金量大吗?
  12. 申请GitHub学生包,0成本搭建WordPress教程
  13. 桂电计算机专业2021推免人数,2021届计算机科学与技术学院推免公示
  14. 2022:股票程序化交易实战2022Q3
  15. 星辉信息科技教程-Centos上silk微信语音转换成mp3格式
  16. 【Tableau Desktop 企业日常技巧12】Tableau安装版本要求和配置 信息
  17. js 前端时间选择器
  18. 程序员文档写作能力(二)-大三段式构架你的文档
  19. 【ElementUI】el-upload 到底该怎么使用?记录有时上传图片会失败,有时上传成功又出现图片闪动的问题
  20. Delphi中record的使用

热门文章

  1. php制作简单的用户登陆,如何用php代码实现简单的用户登陆以及登陆验证功能
  2. java aio聊天_JAVA aio简单使用
  3. 前端必须懂的计算机网络知识—(跨域、代理、本地存储)
  4. Html5移动开发之Localstorage(本地存储)
  5. 发现2017年最好的CSS框架
  6. HTML5 拖放、交换位置
  7. 拖拽功能-jquery
  8. python网络爬虫与信息提取 学习笔记day3
  9. bzoj 4332:JSOI2012 分零食
  10. Java访问权限的范围