Module: Hamster

Defined in:
lib/hamster/set.rb,
lib/hamster/hash.rb,
lib/hamster/trie.rb,
lib/hamster/deque.rb,
lib/hamster/vector.rb,
lib/hamster/nested.rb,
lib/hamster/version.rb,
lib/hamster/undefined.rb,
lib/hamster/immutable.rb,
lib/hamster/associable.rb,
lib/hamster/sorted_set.rb,
lib/hamster/enumerable.rb,
lib/hamster/mutable_hash.rb,
lib/hamster/read_copy_update.rb,
lib/hamster/experimental/mutable_set.rb,
lib/hamster/experimental/mutable_queue.rb

Defined Under Namespace

Modules: Associable, Enumerable Classes: Deque, Hash, MutableHash, MutableQueue, MutableSet, Set, SortedSet, Vector

Constant Summary collapse

VERSION =

Current released gem version. Note that master will often have the same value as a release gem but with different code.

"2.0.0"

Class Method Summary collapse

Class Method Details

.from(obj) ⇒ Hash, ...

Create a Hamster immutable data structure with nested Hamster data structure from a nested Ruby object obj. This method recursively "walks" the Ruby object, converting Ruby Hash to Hash, Ruby Array to Vector, Ruby Set to Set, and Ruby SortedSet to SortedSet. Other objects are left as-is.

Examples:

h = Hamster.from({ "a" => [1, 2], "b" => "c" })
# => Hamster::Hash["a" => Hamster::Vector[1, 2], "b" => "c"]

Returns:


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hamster/nested.rb', line 24

def from(obj)
  case obj
  when ::Hash
    res = obj.map { |key, value| [from(key), from(value)] }
    Hamster::Hash.new(res)
  when Hamster::Hash
    obj.map { |key, value| [from(key), from(value)] }
  when ::Struct
    from(obj.to_h)
  when ::Array
    res = obj.map { |element| from(element) }
    Hamster::Vector.new(res)
  when ::SortedSet
    # This clause must go before ::Set clause, since ::SortedSet is a ::Set.
    res = obj.map { |element| from(element) }
    Hamster::SortedSet.new(res)
  when ::Set
    res = obj.map { |element| from(element) }
    Hamster::Set.new(res)
  when Hamster::Vector, Hamster::Set, Hamster::SortedSet
    obj.map { |element| from(element) }
  else
    obj
  end
end

.to_ruby(obj) ⇒ ::Hash, ...

Create a Ruby object from Hamster data. This method recursively "walks" the Hamster object, converting Hash to Ruby Hash, Vector and Deque to Ruby Array, Set to Ruby Set, and SortedSet to Ruby SortedSet. Other objects are left as-is.

Examples:

h = Hamster.to_ruby(Hamster.from({ "a" => [1, 2], "b" => "c" }))
# => { "a" => [1, 2], "b" => "c" }

Returns:

  • (::Hash, ::Array, ::Set, ::SortedSet, Object)

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hamster/nested.rb', line 61

def to_ruby(obj)
  case obj
  when Hamster::Hash, ::Hash
    obj.each_with_object({}) { |keyval, hash| hash[to_ruby(keyval[0])] = to_ruby(keyval[1]) }
  when Hamster::Vector, ::Array
    obj.each_with_object([]) { |element, arr| arr << to_ruby(element) }
  when Hamster::Set, ::Set
    obj.each_with_object(::Set.new) { |element, set| set << to_ruby(element) }
  when Hamster::SortedSet, ::SortedSet
    obj.each_with_object(::SortedSet.new) { |element, set| set << to_ruby(element) }
  when Hamster::Deque
    obj.to_a.tap { |arr| arr.map! { |element| to_ruby(element) }}
  else
    obj
  end
end