Ruby里的structure和set_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > Ruby里的structure和set

Ruby里的structure和set

 2012/10/15 10:38:48  夜鸣猪  程序员俱乐部  我要评论(0)
  • 摘要:#CreateastructurewithanameinStructStruct.new("Customer",:name,:address)#=>Struct::CustomerStruct::Customer.new("Dave","123Main")#=>#<structStruct::Customername="Dave",address="123Main">#CreateastructurenamedbyitsconstantCustomer=Struct
  • 标签:Ruby
# Create a structure with a name in Struct
Struct.new("Customer", :name, :address)    #=> Struct::Customer
Struct::Customer.new("Dave", "123 Main")   #=> #<struct Struct::Customer name="Dave", address="123 Main">

# Create a structure named by its constant
Customer = Struct.new(:name, :address)     #=> Customer
Customer.new("Dave", "123 Main")           #=> #<struct Customer name="Dave", address="123 Main">


#Array hybrid Hash
require 'set'
s1 = Set.new [1, 2]                   # -> #<Set: {1, 2}>
s2 = [1, 2].to_set                    # -> #<Set: {1, 2}>
s1 == s2                              # -> true
s1.add("foo")                         # -> #<Set: {1, 2, "foo"}>
s1.merge([2, 6])                      # -> #<Set: {6, 1, 2, "foo"}>
s1.subset? s2                         # -> false
s2.subset? s1                         # -> true
发表评论
用户名: 匿名