as_hash ruby_Ruby中的Hash <other_hash(<运算符)

as_hash ruby

In this article, we will see about the implementation of "<" operator. The working is pretty clear with the help of its name. It is not as simple as it seems. We will figure it out in the content of this article. We will understand it with the help of syntaxes and demonstrating program codes.

在本文中,我们将了解“ <”运算符的实现 。 借助其名称,工作非常清晰。 它并不像看起来那么简单。 我们将在本文的内容中找到答案。 我们将借助语法和演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method that is defined in Ruby's library especially for Hash class. This method works in a way that it carries out a comparison between two different hashes and returns a Boolean value. The method returns true when the hash is a subset of another hash and returns false if it is not the subset of another Hash instance. Being a subset simply means to have all those elements which are present in another Hash object.

此方法是在Ruby的库中定义的公共实例方法,特别是针对Hash类。 该方法的工作方式是在两个不同的哈希值之间进行比较并返回一个布尔值。 当该哈希是另一个哈希的子集时,该方法返回true;如果它不是另一个Hash实例的子集,则返回false。 作为子集仅意味着拥有所有存在于另一个Hash对象中的所有元素。

Syntax:

句法:

    Hash < Hash_object -> true or false

Parameter(s) required:

所需参数:

This method does not require any argument.

此方法不需要任何参数。

Example 1:

范例1:

=begin
  Ruby program to demonstrate new method
=end	

hash1 = {"color"=> "Black", "object"=>"phone", "love"=>"mom", "fruit"=>"Kiwi"}

hash2= {"color"=> "Black", "object"=>"phone", "love"=>"mom", "fruit"=>"Kiwi", "vege"=>"potato"}

if(hash1<hash2)
	puts "hash1 is a subset of hash2"
else
	puts "hash1 is not a subset of hash2"
end

Output

输出量

hash1 is a subset of hash2

Explanation:

说明:

In the above code, you can simply observe that the method has returned true inside the if the condition that is because the message is printed as  "hash1 is the subset of hash2". This happened because hash1 is having all the elements which are present in hash2. This is the simple meaning of the subset.

在上面的代码中,您可以简单地观察到,如果条件是因为消息被打印为“ hash1是hash2的子集” ,则该方法在true内返回了true。 发生这种情况是因为hash1具有hash2中存在的所有元素。 这是子集的简单含义。

Example 2:

范例2:

=begin
  Ruby program to demonstrate < operator
=end	

hash1 ={"color"=> "Black", "object"=>"phone", "love"=>"mom","fruit"=>"Kiwi","animal"=>"cow"}

hash2 ={"color"=> "Black", "object"=>"phone", "love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

if(hash1<hash2)
	puts "hash1 is a subset of hash2"
else
	puts "hash1 is not a subset of hash2"
end

Output

输出量

hash1 is not a subset of hash2

Explanation:

说明:

In the above code, you can simply observe that the method has returned false inside the if the condition that is because the message is printed as "hash1 is not a subset of hash2". This happened because hash1 is not having all the elements which are present in hash2. This is the simple meaning of the subset.

在上面的代码中,您可以简单地观察到,如果条件是因为消息被打印为“ hash1不是hash2的子集” ,则该方法在false内部已经返回。 发生这种情况是因为hash1并不具有hash2中存在的所有元素。 这是子集的简单含义。

翻译自: https://www.includehelp.com/ruby/hash-other_hash-operator.aspx

as_hash ruby

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值