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, #only, #protect_keys!, #to_mash, #to_params, #to_xml_attributes, #unprotect_keys!

Constructor Details

#initialize(arr = []) ⇒ Array

Returns The array the Set was initialized with.

Parameters:

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

    Initial set values.



12
13
14
# File 'lib/extlib/simple_set.rb', line 12

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

Instance Method Details

#<<(value) ⇒ TrueClass

Parameters:

  • value (Object)

    Value to add to set.

Returns:



19
20
21
# File 'lib/extlib/simple_set.rb', line 19

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

#inspectString

Returns A human readable version of the set.

Returns:

  • (String)

    A human readable version of the set.



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

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

#merge(arr) ⇒ SimpleSet

Returns The set after the Array was merged in.

Parameters:

  • arr (Array)

    Values to merge with set.

Returns:

  • (SimpleSet)

    The set after the Array was merged in.



26
27
28
# File 'lib/extlib/simple_set.rb', line 26

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