as_hash ruby_Ruby中带有示例的Hash.default_proc方法

news/2024/7/19 13:40:36 标签: python, java, 数据结构, javascript, js

as_hash ruby

Hash.default_proc方法 (Hash.default_proc Method)

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

Method description:

方法说明:

This method is a public instance method that is defined in Ruby library especially for Hash class. This method works in a way that if the hash new method has been invoked with a block then it will return that block when the default_proc() method has been invoked. This method will return nil if the new method has been invoked without passing any block. You will understand the concept in a broader way when you will go through its examples.

此方法是在Ruby库中定义的公共实例方法,特别是针对Hash类。 该方法的工作方式是,如果已使用一个块调用了hash新方法,则当调用default_proc()方法时它将返回该块。 如果在没有传递任何块的情况下调用了新方法,则此方法将返回nil。 当您浏览示例时,将更广泛地理解该概念。

Syntax:

句法:

    Hash_object.default_proc

Argument(s) required:

所需参数:

This method does not require any argument.

此方法不需要任何参数。

Example 1:

范例1:

=begin
  Ruby program to demonstrate default_proc method
=end	

h = Hash.new {|h,k| h[k] = k*k }

puts "default_proc implementation:"   

p = h.default_proc                 
a = []                             

puts "#{p.call(a, 2)}"
puts "The elements in 'a' array are: #{a}" 

Output

输出量

default_proc implementation:
4
The elements in 'a' array are: [nil, nil, 4]

Explanation:

说明:

In the above code, you can observe that we are invoking a new method along with the block and that block is doing nothing rather than returning the double of the element passed. When we are calling the proc p which is containing the default proc value of Hash object along with the index and block value than at the time of printing the array, we are getting the block returned value stored at the index position.

在上面的代码中,您可以观察到我们正在与该块一起调用一个新方法,并且该块什么也不做,而不是返回所传递元素的两倍。 当我们调用proc p时 ,它包含Hash对象的默认proc值以及索引和块值,而不是在打印数组时,它包含存储在索引位置的块返回值。

Example 2:

范例2:

=begin
  Ruby program to demonstrate default_proc method
=end	

h = Hash.new 

puts "default_proc implementation:"   

p = h.default_proc                 
a = []                             

puts "#{p.call(a, 2)}"
puts "The elements in 'a' array are: #{a}"            

Output

输出量

default_proc implementation:
undefined method `call' for nil:NilClass
(repl):12:in `<main>'

Explanation:

说明:

In the above code, you can observe that when the new method is invoked without a block then the variable 'p' is not considered as a proc because the hash default_proc method has returned 'nil'.

在上面的代码中,您可以观察到,在没有块的情况下调用新方法时,变量'p'不被视为proc,因为哈希default_proc方法返回了'nil'

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

as_hash ruby


http://www.niftyadmin.cn/n/1255101.html

相关文章

王道书代码实现(详细个人解析) C++版

第二章顺序表 顺序表基础操作 #define MaxSize 50 typedef struct{int data[MaxSize];int length; }SqList;void InitList(SqList &L){//初始化for(int i0;i<MaxSize;i){L.data[i]0;}L.length0;//初始表长0 }1 从顺序表中删除具有最小值的元素&#xff08;假设唯一&am…

姓名校验正则表达式_spring boot 参数校验这么做简洁实用

文章为本人原创&#xff0c;创作不易&#xff0c;如果对您有帮助&#xff0c;点赞收藏加关注。spring boot 优雅的参数校验最近在公司其它业务组的项目中发现地方很多用if else校验请求参数&#xff0c;几乎每个接收参数的地方都有一大堆参数校验。这篇文章介绍在spring boot项…

kth number_C ++程序切换数字的Kth位

kth numberProgram to toggle the Kth bit of a number in C: Here we are going to use bitwise operators to toggle Kth bit in the binary representation of a given number. 在C 中切换数字的第K位的程序 &#xff1a;在这里我们将使用按位运算符 在给定数字的二进制表示…

day 45 JavaScript 下 函数

主要内容 : https://www.cnblogs.com/liwenzhou/p/8004649.html 正则表达式: a : 两种创建正则表达式的方法: 方法一: var r2 new RegExp(^1[3-9][0-9]{9}$); # 不可以写成\d{9} 要转义//d r2.test(13411119999) true方法二: /^1[3-9][0-9]{9}$/.test(13489889990) #可以…

PHP array_product()函数与示例

PHP array_product()函数 (PHP array_product() function) array_product() function is used to find the product of all elements (values) of an array, this method accepts an array and returns the product of all values. array_product()函数用于查找数组的所有元素(…

04.Django视图函数

Django视图函数 视图函数练掌握两个对象即可&#xff1a;   请求对象(request)和响应对象(HttpResponse) 01.HttpRequest对象&#xff1a;request属性&#xff0c;django将请求报文中的请求行、首部信息、内容主体封装成 HttpRequest 类中的属性。 除了特殊说明的之外&#x…

stl 数组最大元素_分配带有数组元素的列表| C ++ STL

stl 数组最大元素Given an array and we have to create a list, that should be assigned with all elements of the array using C (STL) program. 给定一个数组&#xff0c;我们必须创建一个列表&#xff0c;该列表应使用C (STL)程序分配给该数组的所有元素。 Example: 例&…

jQuery中$.each()方法的使用(从业人员项目--添加产品和修改产品,定价时用到了)...

$.each()是对数组&#xff0c;json和dom结构等的遍历&#xff0c;说一下他的使用方法吧。 1、遍历一维数组 var arr1[aa,bb,cc,dd];$.each(arr1,function(i,val){ //两个参数&#xff0c;第一个参数表示遍历的数组的下标&#xff0c;第二个参数表示下标对应的值console.log(iva…