Class: I8::Set
- Inherits:
-
Hamster::Set
- Object
- Hamster::Set
- I8::Set
- Defined in:
- lib/nrser/labs/i8.rb
Class Method Summary collapse
-
.empty ⇒ I8::Set
Override to build our empty set through alloc so that we can return it in Set.new.
-
.new(items = []) ⇒ I8::Set
Get a Set containing ‘items`.
Methods inherited from Hamster::Set
#as_json, #to_h, #to_mutable, #to_mutable_array, #to_yaml
Class Method Details
.empty ⇒ I8::Set
Override to build our empty set through alloc so that we can return it in new.
82 83 84 |
# File 'lib/nrser/labs/i8.rb', line 82 def self.empty @empty ||= alloc Hamster::Trie.new( 0 ) end |
.new(items = []) ⇒ I8::Set
Get a I8::Set containing ‘items`.
Overridden to…
-
Return ‘items` if items is already a I8::Set… sort of like a copy constructor that doesn’t actually copy because the instances are immutable.
-
Return an instance of ‘self` pointing to `items`’s Hamster::Trie if ‘items` is a Hamster::Set; this way you get an instance of the correct class but don’t do any additional instantiation.
-
Returns empty if ‘items` responds to `#empty?` truthfully.
Otherwise, defers to ‘super`.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/nrser/labs/i8.rb', line 62 def self.new items = [] case items when self items when Hamster::Set alloc items.instance_variable_get( :@trie ) else if items.respond_to?( :empty? ) && items.empty? self.empty else super items end end end |