Set #add(别名<<)
require 'set'
s1=Set[3,4,5]
s2=Set[3,1,5]
p s1.union(s2)
p s1+s2
p s1|s2#<Set: {5, 1, 3, 4}>
#<Set: {5, 1, 3, 4}>
#<Set: {5, 1, 3, 4}>
交集:Set#intersection(别名:&)require 'set'
s1=Set[3,4,5]
s2=Set[3,1,5]
p s1.intersection(s2)
p s1&s2#<Set: {5, 3}>
#<Set: {5, 3}>
差集:Set#-require 'set'
s1=Set[3,4,5]
s2=Set[3,1,5]
p s1-s2
p s2-s1#<Set: {4}>
#<Set: {1}>
Set#member?? ,? Set#include?require 'set'
s1=Set[3,4,5]
s2=Set[3,1,5]
p s1.include?(3)
p s2.member?(5)true
trueSet#empty?? , Set#clear?
require 'set'
s1=Set[3,4,5]
s2=Set[3,1,5]
p s1.empty?
s1.clear
p s1.empty?
false
true
测试两个集合的关系:1.接收方是否为另一个集合的子集?真子集?超集?require 'set'
x = Set[3,4,5]
y = Set[3,4]
p x.subset?(y)??????????????????#x是否y的子集?????? ?否
p y.subset?(x)??????????????????#y是否x的子集????????是
p y.proper_subset?(x)????????#y是否x的真子集????? 是
p x.subset?(x)??????????????????#x是不是本身的子集? 是
p x.proper_subset?(x)????????#x是否本身的真子集? 否
p x.superset?(y)???????????????#x是否y的超集?????? ??是Set#add(别名<<):往集合中添加元素,返回添加后的集合。 merge方法用于合并两个集合(并集)require 'set'
x = Set[3,4,5]
y= Set[1,2,3]
p x<<5
p x.merge(y)#<Set: {5, 3, 4}>
#<Set: {5, 1, 2, 3, 4}>
----------------------------?
本文出自 “李骥平” 博客,请务必保留此出处http://fsjoy.blog.51cto.com/318484/70176