---
title: "Ruby里的structure和set"
author: "夜鸣猪"
date: "2012-09-10T06:29:28Z"
tags: []
categories: ["编程语言"]
slug: "ruby-structure-set"
draft: false
lastmod: "2012-09-10T06:29:28Z"
id: 1674760
url: "https://hlee.iteye.com/blog/1674760"
---

```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">

```



```ruby

#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
```