Class: Extlib::SimpleSet

Inherits:
Hash show all
Defined in:
lib/extlib/simple_set.rb

Overview

Simple set implementation on top of Hash with merging support.

In particular this is used to store a set of callable actions of controller.

Instance Method Summary collapse

Methods inherited from Hash

#add_html_class!, #environmentize_keys!, #except, from_xml, #normalize_param, #only, #protect_keys!, #to_mash, #to_params, #to_xml_attributes, #unprotect_keys!

Constructor Details

#initialize(arr = []) ⇒ Array

Create a new SimpleSet containing the unique members of arr

Parameters:

  • arr (Array) (defaults to: [])

    Initial set values.



17
18
19
# File 'lib/extlib/simple_set.rb', line 17

def initialize(arr = [])
  Array(arr).each {|x| self[x] = true}
end

Instance Method Details

#<<(value) ⇒ SimpleSet

Add a value to the set, and return it

Parameters:

  • value (Object)

    Value to add to set.

Returns:



29
30
31
32
# File 'lib/extlib/simple_set.rb', line 29

def <<(value)
  self[value] = true
  self
end

#inspectString

Get a human readable version of the set.

s = SimpleSet.new([:a, :b, :c])
s.inspect                 #=> "#<SimpleSet: {:c, :a, :b}>"

Returns:

  • (String)

    A human readable version of the set.



58
59
60
# File 'lib/extlib/simple_set.rb', line 58

def inspect
  "#<SimpleSet: {#{keys.map {|x| x.inspect}.join(", ")}}>"
end

#merge(arr) ⇒ SimpleSet

Merge arr with receiver, producing the union of receiver & arr

s = Extlib::SimpleSet.new([:a, :b, :c])
s.merge([:c, :d, :e, f])  #=> #<SimpleSet: {:e, :c, :f, :a, :d, :b}>

Parameters:

  • arr (Array)

    Values to merge with set.

Returns:

  • (SimpleSet)

    The set after the Array was merged in.



45
46
47
# File 'lib/extlib/simple_set.rb', line 45

def merge(arr)
  super(arr.inject({}) {|s,x| s[x] = true; s })
end