ruby .set函数_Ruby Set比较

ruby .set函数

在Ruby中进行比较 (Set comparison in Ruby)

Ruby provides you various methods through which you can carry out comparison operations within two sets. Now, let us go through various methods and understand their implementation with the help of their syntax and examples.

Ruby为您提供了各种方法,您可以通过它们在两组中执行比较操作 。 现在,让我们看一下各种方法,并借助它们的语法和示例了解它们的实现。

==运算子 (== operator)

This operator returns true when both the sets are equal or you can say that for the value to be true, set2 must be having the same values as of set1.

当两个集合相等时,该运算符将返回true;或者您可以说,要使该值为true,则set2必须具有与set1相同的值。

Syntax:

句法:

    Set1 == Set2

Example:

例:

=begin
Ruby program to show ==
=end

require 'set'

Vegetable = Set.new(["potato", "tomato","brinjal","onion"])

Sabzi = Set.new(["potato", "tomato","brinjal","onion"])

bool = Vegetable == Sabzi

if bool == true
	puts "Vegetable is equal to Sabzi"
else
	puts "Vegetable is not equal to Sabzi"
end

Output

输出量

Vegetable is equal to Sabzi

<运算符 (< operator)

This operator is an alias for the proper subset. This operator will return true when Set2 is having all the elements which are present in Set1. Set2 may or may not have more elements.

该运算符是适当子集的别名。 当Set2具有Set1中存在的所有元素时,此运算符将返回true。 Set2可以具有或可以不具有更多元素。

Syntax:

句法:

    Set2 < Set1

Example:

例:

=begin
Ruby program to show < operator
=end

require 'set'

Vegetable = Set.new(["potato" , "tomato" , "brinjal" , "onion"])

Sabzi = Set.new(["potato" , "tomato" , "brinjal" , "onion" , "beetroot"])

bool = Vegetable < Sabzi

if bool == true
	puts "Vegetable is a subset of Sabzi"
else
	puts "Vegetable is not a subset to Sabzi"
end

>运算子 (> operator)

This operator is an alias for a proper superset. This operator will return true when Set1 is having all the elements which are present in Set2. Set1 may or may not have more elements.

该运算符是适当超集的别名。 当Set1具有Set2中存在的所有元素时,此运算符将返回true。 Set1可能有也可能没有更多元素。

Syntax:

句法:

    Set2 > Set1

Example:

例:

=begin
Ruby program to show > operator
=end

require 'set'

Vegetable = Set.new(["potato", "tomato","brinjal","onion"])

Sabzi = Set.new(["potato", "tomato","brinjal","onion","beetroot"])

bool = Vegetable > Sabzi

if bool == true
	puts "Vegetable is a superset of Sabzi"
else
	puts "Vegetable is not a superset of Sabzi"
end

Output

输出量

Vegetable is not a superset of Sabzi

.subset?() (.subset?())

This operator is an alias for a proper superset. This operator will return true when Set1 is having all the elements which are present in Set2. Set1 may or may not have more elements.

该运算符是适当超集的别名。 当Set1具有Set2中存在的所有元素时,此运算符将返回true。 Set1可能有也可能没有更多元素。

Syntax:

句法:

    set1.subset?(set2)

Example:

例:

=begin
Ruby program to show subset method
=end

require 'set'

Vegetable = Set.new(["potato", "tomato","brinjal","onion","peas"])

Sabzi = Set.new(["potato", "tomato","brinjal","onion","beetroot"])

bool = Vegetable.subset?(Sabzi)

if bool == true
	puts "Vegetable is a subset of Sabzi"
else
	puts "Vegetable is not a subset of Sabzi"
end

Output

输出量

Vegetable is not a subset of Sabzi

.superset?() (.superset?())

The working of this method is the same as of < operator as > operator is an alias for this method. This method is implemented in the following manner:

此方法的工作方式与<运算符相同,因为>运算符是此方法的别名。 此方法以以下方式实现:

Syntax:

句法:

    set1.superset?(set2)

Example:

例:

=begin
Ruby program to show superset method
=end

require 'set'

Vegetable = Set.new(["potato", "tomato","brinjal","onion","peas","beetroot"])

Sabzi = Set.new(["potato", "tomato","brinjal","onion","beetroot"])

bool = Vegetable.superset?(Sabzi)

if bool == true
	puts "Vegetable is a superset of Sabzi"
else
	puts "Vegetable is not a superset of Sabzi"
end

Output

输出量

Vegetable is a superset of Sabzi


翻译自: https://www.includehelp.com/ruby/set-comparison.aspx

ruby .set函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值