Class: Extlib::SimpleSet

Inherits:
Hash show all
Defined in:
lib/gems/extlib-0.9.9/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.

Constant Summary

Constants included from ActiveSupport::CoreExtensions::Hash::Conversions

ActiveSupport::CoreExtensions::Hash::Conversions::XML_FORMATTING, ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING, ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES

Instance Method Summary collapse

Methods inherited from Hash

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

Methods included from ActiveSupport::CoreExtensions::Hash::Except

#except, #except!

Methods included from ActiveSupport::CoreExtensions::Hash::Slice

#slice, #slice!

Methods included from ActiveSupport::CoreExtensions::Hash::Diff

#diff

Methods included from ActiveSupport::CoreExtensions::Hash::Conversions

included, #to_query, #to_xml

Methods included from ActiveSupport::CoreExtensions::Hash::ReverseMerge

#reverse_merge, #reverse_merge!

Methods included from ActiveSupport::CoreExtensions::Hash::DeepMerge

#deep_merge, #deep_merge!

Methods included from ActiveSupport::CoreExtensions::Hash::IndifferentAccess

#with_indifferent_access

Methods included from ActiveSupport::CoreExtensions::Hash::Keys

#assert_valid_keys, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_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/gems/extlib-0.9.9/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/gems/extlib-0.9.9/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/gems/extlib-0.9.9/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/gems/extlib-0.9.9/lib/extlib/simple_set.rb', line 45

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