Class: Tash

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/tash.rb,
lib/tash/version.rb

Overview

A Tash is a hash with transformed keys.

Constant Summary collapse

VERSION =
'1.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&transform) ⇒ Tash

Returns a new empty Tash object.

Examples:

Tash.new { |key| key.to_s.downcase }

Parameters:

  • transform (Proc)

    receives a key and transforms it as desired before using the key



77
78
79
80
81
# File 'lib/tash.rb', line 77

def initialize(&transform)
  @transform = transform
  @ir = {} # internal representation - @ir[transformed key] = value
  @default_proc = nil
end

Class Method Details

.[](*objects, &transform) ⇒ Tash

Returns a new Tash object populated with the given objects, if any. If a Tash is passed with no block it returns a duplicate with the transform block from the original. If a Tash is passed with a block it is treated as a Hash.

Examples:

Empty

Tash[] # => {}

Given a Tash with no block

t = Tash[FOO: 1, BAR: 2, &:downcase]
t2 = Tash[t]
t2[:BAZ] = 3
t2 # => {:foo=>1, :bar=>2, :baz=>3}

Given a Hash

Tash[FOO: 1, BAR: 2, &:downcase] # => {:foo=>1, :bar=>2}

Given an even number of objects

Tash[:FOO, 1, :BAR, 2, &:downcase] # => {:foo=>1, :bar=>2}

Parameters:

  • *objects (Array<Objects>)

    A Tash, Hash, or even number of objects

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tash.rb', line 40

def self.[](*objects, &transform) # rubocop:disable Metrics/PerceivedComplexity
  if objects.empty?
    new(&transform)
  elsif objects.size == 1 && !transform && objects.first.is_a?(self)
    objects.first.dup
  elsif objects.size == 1 && objects.first.respond_to?(:to_hash)
    from_hash(objects.first.to_hash, &transform)
  elsif objects.size.even?
    from_array(objects, &transform)
  else
    raise ArgumentError, "odd number of arguments for #{name}"
  end
end

Instance Method Details

#<(other) ⇒ true or false

Returns true if tash is a proper subset of other, false otherwise.

Examples:

t1 = Tash[foo: 0, bar: 1]
t2 = Tash[foo: 0, bar: 1, baz: 2]
t1 < t2 # => true
t2 < t1 # => false
t1 < t1 # => false

Parameters:

  • other (Tash, Hash)

Returns:

  • (true or false)


# File 'lib/tash.rb', line 108


#<=(other) ⇒ true or false

Returns true if tash is a subset of other, false otherwise.

Examples:

t1 = Tash[foo: 0, bar: 1]
t2 = Tash[foo: 0, bar: 1, baz: 2]
t1 <= t2 # => true
t2 <= t1 # => false
t1 <= t1 # => true

Parameters:

  • other (Tash, Hash)

Returns:

  • (true or false)


# File 'lib/tash.rb', line 122


#==(other) ⇒ true or false

Returns true if all of the following are true:

  • object is a Tash object.
  • tash and object have the same keys (regardless of order).
  • For each key key, tash[key] == other[key].

Otherwise, returns false.

Examples:

t1 = Tash[foo: 0, bar: 1, baz: 2]
t2 = Tash[foo: 0, bar: 1, baz: 2]
t1 == t2 # => true
h3 = Tash[baz: 2, bar: 1, foo: 0]
t1 == h3 # => true

Parameters:

  • other (Object)

Returns:

  • (true or false)


154
155
156
157
158
# File 'lib/tash.rb', line 154

def ==(other)
  return false unless other.is_a?(self.class)

  @ir == other.to_hash
end

#>(other) ⇒ true or false

Returns true if tash is a proper superset of other, false otherwise.

Examples:

t1 = Tash[foo: 0, bar: 1, baz: 2]
t2 = Tash[foo: 0, bar: 1]
t1 > t2 # => true
t2 > t1 # => false
t1 > t1 # => false

Parameters:

  • other (Tash, Hash)

Returns:

  • (true or false)


# File 'lib/tash.rb', line 160


#>=(other) ⇒ true or false

Returns true if tash is a superset of other, false otherwise.

Examples:

t1 = Tash[foo: 0, bar: 1, baz: 2]
t2 = Tash[foo: 0, bar: 1]
t1 >= t2 # => true
t2 >= t1 # => false
t1 >= t1 # => true

Parameters:

  • other (Tash, Hash)

Returns:

  • (true or false)


# File 'lib/tash.rb', line 174


#[](key) ⇒ value

Returns the value associated with the given key after transformation, if found.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t[:FOO] # => 0

Not found key with a default value

t = Tash[foo: 0, bar: 1, baz: 2]
t.default = 1_000
t[:nosuch] # => 1_000

Parameters:

  • key (Object)

Returns:

  • (value)


203
204
205
# File 'lib/tash.rb', line 203

def [](key)
  @ir[transform(key)]
end

#[]=(key, value) ⇒ value Also known as: store

Associates the given value with the given key after transformation. If the given post transformation key exists, replaces its value with the given value; the ordering is not affected. If post transformation key does not exist, adds the transformed key and value; the new entry is last in the order.

Examples:

t = Tash[Foo: 0, Bar: 1, &:downcase]
t[:FOO] = 2 # => 2
t.store(:bar, 3) # => 3
t[:Bat] = 4 # => 4
t # => {:foo=>2, :bar=>3, :bat=>4}

Parameters:

  • key (Object)
  • value (Object)

Returns:

  • (value)


224
225
226
# File 'lib/tash.rb', line 224

def []=(key, value)
  @ir[transform(key)] = value
end

#assoc(key) ⇒ Array<K,V> or nil

If the given transformed key is found, returns a 2-element Array containing that key and its value. Returns nil if the tranformed key key is not found.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.assoc(:bar) # => [:bar, 1]

Parameters:

  • key (Object)

Returns:

  • (Array<K,V> or nil)


240
241
242
# File 'lib/tash.rb', line 240

def assoc(key)
  @ir.assoc(transform(key))
end

#clearself

Removes all tash entries.

Returns:

  • (self)


247
248
249
250
# File 'lib/tash.rb', line 247

def clear
  @ir.clear
  self
end

#compactTash

Returns a copy of self with all nil-valued entries removed.

Examples:

t = Tash[foo: 0, bar: nil, baz: 2, bat: nil]
t1 = t.compact
t1 # => {:foo=>0, :baz=>2}

Returns:



260
261
262
# File 'lib/tash.rb', line 260

def compact
  new_from_self(@ir.compact)
end

#compact!self or nil

Returns self with all its nil-valued entries removed.

Examples:

t = Tash[foo: 0, bar: nil, baz: 2, bat: nil]
t.compact! # => {:foo=>0, :baz=>2}

Returns:

  • (self or nil)


271
272
273
# File 'lib/tash.rb', line 271

def compact!
  self if @ir.compact!
end

#compare_by_identityself

Sets self to consider only identity in comparing keys; two keys are considered the same only if they are the same object.

Examples:

Before being set

s0 = 'x'
s1 = 'x'
t = Tash.new
t.compare_by_identity? # => false
t[s0] = 0
t[s1] = 1
t # => {"x"=>1}

After being set

t = Tash.new
t.compare_by_identity # => {}
t.compare_by_identity? # => true
t[s0] = 0
t[s1] = 1
t # => {"x"=>0, "x"=>1}

Returns:

  • (self)


296
297
298
299
# File 'lib/tash.rb', line 296

def compare_by_identity
  @ir.compare_by_identity
  self
end

#compare_by_identity?Boolean

Returns true if #compare_by_identity has been called, false otherwise.

Returns:

  • (Boolean)


# File 'lib/tash.rb', line 301


#deconstruct_keys(keys) ⇒ Hash

Provides support for pattern matching using a Tash. Pattern keys will be transformed before matching.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
case t
in { foo: x, BAR: 1 }
  x
else
  nil
end # => 0

Returns:

  • (Hash)


320
321
322
323
324
# File 'lib/tash.rb', line 320

def deconstruct_keys(keys)
  return {} if keys.nil?

  keys.to_h { |key| [key, self[key]] }
end

#defaultObject #default(key) ⇒ Object

Returns the default value for the given transformed key. The returned value will be determined either by the default proc or by the default value.

Examples:

t = Tash.new
t.default # => nil

With a key

t = Tash[Foo: 0]
t.default_proc = proc { |tash, key| tash[k] = "No key #{key}" }
t[:foo] = "Hello"
t.default(:FOO) # => "No key foo"

Parameters:

  • key (Object)

Returns:

  • (Object)


346
347
348
349
350
351
352
353
354
355
# File 'lib/tash.rb', line 346

def default(*key)
  case key.size
  when 0
    @ir.default
  when 1
    @ir.default(transform(key.first))
  else
    @ir.default(*key)
  end
end

#default=(value) ⇒ Object

Sets the default value to value.

Examples:

t = Tash.new
t.default # => nil
t.default = false # => false
t.default # => false

Parameters:

  • value (Object)

Returns:

  • (Object)


# File 'lib/tash.rb', line 357


#default_procProc or nil

Returns the default proc for self.

Examples:

t = Tash.new
t.default_proc # => nil
t.default_proc = proc { |tash, key| "Default value for #{key}" }
t.default_proc.class # => Proc

Returns:

  • (Proc or nil)


379
380
381
# File 'lib/tash.rb', line 379

def default_proc # rubocop:disable Style/TrivialAccessors (I want it to show as a method in the docs.)
  @default_proc
end

#default_proc=(proc) ⇒ Proc

Sets the default proc for self to proc.

Examples:

t = Tash.new
t.default_proc # => nil
t.default_proc = proc { |tash, key| "Default value for #{key}" }
t.default_proc.class # => Proc
t.default_proc = nil
t.default_proc # => nil

Parameters:

  • proc (Proc)

    receives self and a transformed key

Returns:

  • (Proc)


398
399
400
401
402
# File 'lib/tash.rb', line 398

def default_proc=(prok)
  @default_proc = prok

  @ir.default_proc = proc { |_, k| prok.call(self, transform(k)) }
end

#delete(key, &block) ⇒ value or nil, Object

Deletes the entry for the given transformed key and returns its associated value.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.delete(:bar) # => 1
t.delete(:bar) # => nil
t # => {:foo=>0, :baz=>2}

With a block and a key found.

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.delete(:baz) { |key| raise 'Will never happen'} # => 2
t # => {:foo=>0, :bar=>1}

With a block and no key found.

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.delete(:nosuch) { |key| "Key #{key} not found" } # => "Key nosuch not found"
t # => {:foo=>0, :bar=>1, :baz=>2}

Parameters:

  • key (Object)
  • block (Proc)

    receives a transformed key

Returns:

  • (value or nil, Object)


427
428
429
# File 'lib/tash.rb', line 427

def delete(key, &block)
  @ir.delete(transform(key), &block)
end

#delete_if(&block) ⇒ Enumerator, self

If a block given, calls the block with each key-value pair; deletes each entry for which the block returns a truthy value.

Examples:

Without block

t = Tash[foo: 0, bar: 1, baz: 2]
e = t.delete_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:delete_if>
e.each { |key, value| value > 0 } # => {:foo=>0}

With block

t = Tash[foo: 0, bar: 1, baz: 2]
t.delete_if { |key, value| value > 0 } # => {:foo=>0}

Parameters:

  • block (Proc)

    receives a transformed key and value

Returns:

  • (Enumerator, self)


446
447
448
449
450
451
# File 'lib/tash.rb', line 446

def delete_if(&block)
  return to_enum(:delete_if) unless block

  @ir.delete_if(&block)
  self
end

#dig(key, *identifiers) ⇒ Object

Finds and returns the object in nested objects that is specified by transformed key and identifiers. The nested objects may be instances of various classes. This method will use the default values for keys that are not present.

Examples:

Nested Tashes

t = Tash[Foo: Tash[Bar: 2, &:downcase], &:downcase]
t.dig(:foo) # => {:bar=>2}
t.dig(:foo, :bar) # => 2
t.dig(:foo, :bar, :BAZ) # => nil

Nested Arrays

t = Tash[foo: [:a, :b, :c]]
t.dig(:foo, 2) # => :c

Default values

t = Tash[foo: Tash[bar: [:a, :b, :c]]]
t.dig(:hello) # => nil
t.default_proc = -> (tash, _key) { tash }
t.dig(:hello, :world) # => t
t.dig(:hello, :world, :foo, :bar, 2) # => :c

Parameters:

  • key (Object)
  • *identifiers (Object)

Returns:

  • (Object)


479
480
481
# File 'lib/tash.rb', line 479

def dig(key, *identifiers)
  @ir.dig(transform(key), *identifiers)
end

#each(&block) ⇒ Enumerator, self Also known as: each_pair

Calls the given block with each key-value pair. Returns a new Enumerator if no block is given.

Examples:

Without block

t = Tash[foo: 0, bar: 1, baz: 2]
t.each # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each>

With block

t = Tash[foo: 0, bar: 1, baz: 2]
t.each {|key, value| puts "#{key}: #{value}"}
#=> foo: 0
#=> bar: 1
#=> baz: 2

Parameters:

  • block (Proc)

    receives a transformed key and the value

Returns:

  • (Enumerator, self)


500
501
502
503
504
505
# File 'lib/tash.rb', line 500

def each(&block)
  return to_enum(:each) unless block

  @ir.each(&block)
  self
end

#each_key(&block) ⇒ Enumerator, self

Calls the given block with each key.

Examples:

Without block

t = Tash[foo: 0, bar: 1, baz: 2]
t.each_key # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_key>

With block

t = Tash[foo: 0, bar: 1, baz: 2]
t.each_key {|key| puts key}
#=> foo
#=> bar
#=> baz

Parameters:

  • block (Proc)

    receives a transformed key

Returns:

  • (Enumerator, self)


524
525
526
527
528
529
# File 'lib/tash.rb', line 524

def each_key(&block)
  return to_enum(:each_key) unless block

  @ir.each_key(&block)
  self
end

#each_value(&block) ⇒ Enumerator, self

Calls the given block with each value.

Examples:

Without block

t = Tash[foo: 0, bar: 1, baz: 2]
t.each_value # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:each_value>

With block

t = Tash[foo: 0, bar: 1, baz: 2]
t.each_value {|value| puts value}
#=> 0
#=> 1
#=> 2

Parameters:

  • block (Proc)

    receives a value

Returns:

  • (Enumerator, self)


547
548
549
550
551
552
# File 'lib/tash.rb', line 547

def each_value(&block)
  return to_enum(:each_value) unless block

  @ir.each_value(&block)
  self
end

#empty?true or false

Returns true if there are no tash entries, false otherwise.

Examples:

Tash[].empty? # => true
Tash[foo: 0, bar: 1, baz: 2].empty? # => false

Returns:

  • (true or false)


# File 'lib/tash.rb', line 554


#eql?(other) ⇒ true or false

Returns true if all of the following are true:

  • object is a Tash object.
  • tash and object have the same keys (regardless of order).
  • For each key key, tash[key] eql? other[key].

Otherwise, returns false.

Examples:

t1 = Tash[foo: 0, bar: 1, baz: 2]
t2 = Tash[foo: 0, bar: 1, baz: 2]
t1.eql? t2 # => true
h3 = Tash[baz: 2, bar: 1, foo: 0]
t1.eql? h3 # => true

Parameters:

  • other (Object)

Returns:

  • (true or false)


581
582
583
584
585
# File 'lib/tash.rb', line 581

def eql?(other)
  return false unless other.is_a?(self.class)

  @ir.eql?(other.to_hash)
end

#except(*keys) ⇒ Tash

Returns a new Tash excluding entries for the given keys. Any given keys that are not found are ignored. The transform proc is copied to the new Tash.

Examples:

t = Tash[a: 100, b: 200, c: 300]
t.except(:a) #=> {:b=>200, :c=>300}

Parameters:

  • *keys (Array<Object>)

Returns:



598
599
600
# File 'lib/tash.rb', line 598

def except(*keys)
  new_from_self(@ir.except(*keys))
end

#fetch(key) ⇒ Object #fetch(key, default_value) ⇒ Object

Note:

This method does not use the values of either default or default_proc.

Returns the value for the given key, if found. Raises KeyError if neither default_value nor a block was given.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.fetch(:bar) # => 1

With a default

Tash.new.fetch(:nosuch, :default) # => :default

With a default block

Tash.new.fetch(:NOSUCH) {|key| "No key #{key}"} # => "No key nosuch"

Parameters:

  • key (Object)
  • default_value (Object)
  • block (Proc)

    receives a transformed key

Returns:

  • (Object)

Raises:

  • (KeyError)

    When key is not found and no default is provided.



628
629
630
631
632
633
634
# File 'lib/tash.rb', line 628

def fetch(key, *default_value, &block)
  if block
    @ir.fetch(transform(key), &block)
  else
    @ir.fetch(transform(key), *default_value)
  end
end

#fetch_values(*keys, &block) ⇒ Array

Returns a new Array containing the values associated with the given keys *keys. When a block is given, calls the block with each missing transformed key, treating the block's return value as the value for that key.

Examples:

Without a block

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.fetch_values(:baz, :foo) # => [2, 0]

With a block

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
values = t.fetch_values(:bar, :foo, :bad, :bam) {|key| key.to_s}
values # => [1, 0, "bad", "bam"]

Parameters:

  • *keys (Array<Object>)
  • block (Proc)

    receives a transformed key

Returns:

  • (Array)


653
654
655
# File 'lib/tash.rb', line 653

def fetch_values(*keys, &block)
  @ir.fetch_values(*keys.map { |k| transform(k) }, &block)
end

#flattenArray #flatten(level) ⇒ Array

Returns a new Array object that is a 1-dimensional flattening of self.

Examples:

t = Tash[foo: 0, bar: [:bat, 3], baz: 2]
t.flatten # => [:foo, 0, :bar, [:bat, 3], :baz, 2]

level > 1

t = Tash[foo: 0, bar: [:bat, [:baz, [:bat, ]]]]
t.flatten(1) # => [:foo, 0, :bar, [:bat, [:baz, [:bat]]]]
t.flatten(2) # => [:foo, 0, :bar, :bat, [:baz, [:bat]]]
t.flatten(3) # => [:foo, 0, :bar, :bat, :baz, [:bat]]
t.flatten(4) # => [:foo, 0, :bar, :bat, :baz, :bat]

negative levels flatten everything

t = Tash[foo: 0, bar: [:bat, [:baz, [:bat, ]]]]
t.flatten(-1) # => [:foo, 0, :bar, :bat, :baz, :bat]
t.flatten(-2) # => [:foo, 0, :bar, :bat, :baz, :bat]

level == 0 is the same as to_a

t = Tash[foo: 0, bar: [:bat, 3], baz: 2]
t.flatten(0) # => [[:foo, 0], [:bar, [:bat, 3]], [:baz, 2]]
t.flatten(0) == t.to_a # => true

Parameters:

  • level (Integer)

Returns:

  • (Array)


# File 'lib/tash.rb', line 657


#hashInteger

Returns the Integer hash-code for the hash. Two Hash objects have the same hash-code if their content is the same (regardless or order).

Examples:

t1 = Tash[foo: 0, bar: 1, baz: 2]
t2 = Tash[baz: 2, bar: 1, foo: 0]
t2.hash == t1.hash # => true
t2.eql? h1 # => true

Returns:

  • (Integer)


# File 'lib/tash.rb', line 688


#inspectString Also known as: to_s

Returns a new String containing the tash entries.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.inspect # => "{:foo=>0, :bar=>1, :baz=>2}"

Returns:

  • (String)


# File 'lib/tash.rb', line 700


#invertTash

Returns a new Tash object with the each key-value pair inverted. The values will be processed using the key transformation.

Examples:

t = Tash[foo: 'Foo', bar: 'Bar', baz: 'Baz', &:downcase]
t1 = t.invert
t1 # => {'foo'=>:foo, 'bar'=>:bar, 'baz'=>:baz}

Returns:



718
719
720
721
722
723
# File 'lib/tash.rb', line 718

def invert
  new_ir = @ir.invert
  new_ir.transform_keys! { |k| transform(k) }

  new_from_self(new_ir)
end

#keep_if(&block) ⇒ Enumerator, self

Calls the block for each key-value pair; retains the entry if the block returns a truthy value; otherwise deletes the entry.

Examples:

Without block

t = Tash[foo: 0, bar: 1, baz: 2]
e = t.keep_if # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:keep_if>
e.each { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}

With block

t = Tash[foo: 0, bar: 1, baz: 2]
t.keep_if { |key, value| key.start_with?('b') } # => {:bar=>1, :baz=>2}

Parameters:

  • block (Proc)

    receives a transformed key and value

Returns:

  • (Enumerator, self)


740
741
742
743
744
745
# File 'lib/tash.rb', line 740

def keep_if(&block)
  return to_enum(:keep_if) unless block

  @ir.keep_if(&block)
  self
end

#key(value) ⇒ key or nil

Returns the transformed key for the first-found entry with the given value. Returns nil if the key is not found.

Examples:

t = Tash[foo: 0, bar: 2, baz: 2]
t.key(0) # => :foo
t.key(2) # => :bar

Parameters:

  • value (Object)

Returns:



# File 'lib/tash.rb', line 747


#key?(key) ⇒ true or false Also known as: has_key?, include?, member?

Returns true if key after transformation is a key in self, otherwise false.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.key?(:FOO) # => true
t.key?(:bat) # => false

Parameters:

  • key (Object)

Returns:

  • (true or false)


771
772
773
# File 'lib/tash.rb', line 771

def key?(key)
  @ir.key?(transform(key))
end

#keysArray

Returns a new Array containing all keys in self.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.keys # => [:foo, :bar, :baz]

Returns:

  • (Array)


# File 'lib/tash.rb', line 778


#merge(*others, &block) ⇒ Tash

Returns the new Tash formed by merging each of other_tashes_or_hashes into a copy of self.

Each argument in other_tashes_or_hashes must be a Tash or Hash.

With arguments and no block:

  • Returns the new Tash object formed by merging each successive item in other_tashes_or_hashes into self.
  • Each new-key entry is added at the end.
  • Each duplicate-key entry's value overwrites the previous value.

With arguments and a block:

  • Returns a new Tash object that is the merge of self and each given tash or hash.
  • The given tashes or hashes are merged left to right.
  • Each new-key entry is added at the end.
  • For each duplicate key:
    • Calls the block with the transformed key and the old and new values.
    • The block's return value becomes the new value for the entry.

With no arguments:

  • Returns a copy of self.
  • The block, if given, is ignored.

Examples:

With arguments and no block

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t1 = Tash[bat: 3, bar: 4]
h = {BAM: 5, BAT: 6}
t.merge(t1, h) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}

With arguments and a block

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t1 = Tash[bat: 3, bar: 4]
h = {BAM: 5, BAT: 6}
t2 = t.merge(t1, h) { |key, old_value, new_value| old_value + new_value }
t2 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}

With no arguments

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.merge # => {:foo=>0, :bar=>1, :baz=>2}
t1 = t.merge { |key, old_value, new_value| raise 'Cannot happen' }
t1 # => {:foo=>0, :bar=>1, :baz=>2}

Parameters:

  • *others (Tash or Hash)
  • block (Proc)

    receives a transformed key, the old value, and the new value

Returns:



838
839
840
841
842
843
# File 'lib/tash.rb', line 838

def merge(*others, &block)
  new_ir = others.each_with_object(@ir.dup) do |other, ir|
    ir.merge!(other.to_hash.transform_keys { |k| transform(k) }, &block)
  end
  new_from_self(new_ir)
end

#merge!(*others, &block) ⇒ self Also known as: update

Merges each of other_tashes_or_hashes into self.

Each argument in other_tashes_or_hashes must be a Tash or Hash.

With arguments and no block:

  • Returns self, after the given tashes and hashes are merged into it.
  • The given tashes and hashes are merged left to right.
  • Each new entry is added at the end.
  • Each duplicate-key entry's value overwrites the previous value.

With arguments and a block:

  • Returns self, after the given tashes and hashes are merged.
  • The given tashes and hashes are merged left to right.
  • Each new-key entry is added at the end.
  • For each duplicate key:
    • Calls the block with the transformed key and the old and new values.
    • The block's return value becomes the new value for the entry.

With no arguments:

  • Returns self, unmodified.
  • The block, if given, is ignored.

Examples:

With arguments and no block

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t1 = Tash[bat: 3, bar: 4]
h = {BAM: 5, BAT: 6}
t.merge!(t1, h) # => {:foo=>0, :bar=>4, :baz=>2, :bat=>6, :bam=>5}

With arguments and a block

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t1 = Tash[bat: 3, bar: 4]
h = {BAM: 5, BAT: 6}
t2 = t.merge!(t1, h) { |key, old_value, new_value| old_value + new_value }
t2 # => {:foo=>0, :bar=>5, :baz=>2, :bat=>9, :bam=>5}

With no arguments

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.merge # => {:foo=>0, :bar=>1, :baz=>2}
t1 = t.merge! { |key, old_value, new_value| raise 'Cannot happen' }
t1 # => {:foo=>0, :bar=>1, :baz=>2}

Parameters:

  • *others (Tash or Hash)
  • block (Proc)

    receives a transformed key, the old value, and the new value

Returns:

  • (self)


894
895
896
897
898
899
# File 'lib/tash.rb', line 894

def merge!(*others, &block)
  others.each do |other|
    @ir.merge!(other.to_hash.transform_keys { |k| transform(k) }, &block)
  end
  self
end

#rassoc(value) ⇒ Array<K,V> or nil

Returns a new 2-element Array consisting of the key and value of the first-found entry whose value is == to value. Returns nil if no such value found.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.rassoc(1) # => [:bar, 1]

Parameters:

  • value (Object)

Returns:

  • (Array<K,V> or nil)


# File 'lib/tash.rb', line 902


#rehashself

Rebuilds the hash table by recomputing the hash index for each key. The hash table becomes invalid if the hash value of a key has changed after the entry was created.

Returns:

  • (self)


# File 'lib/tash.rb', line 915


#reject(&block) ⇒ Enumerator, Tash

Returns a new Tash object whose entries are all those from self for which the block returns false or nil. Returns a new Enumerator if no block given.

Examples:

Without a block

t = Tash[foo: 0, bar: 1, baz: 2]
e = t.reject # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject>
t1 = e.each { |key, value| key.start_with?('b') }
t1 # => {:foo=>0}

With a block

t = Tash[foo: 0, bar: 1, baz: 2]
t1 = t.reject { |key, value| key.start_with?('b') }
t1 # => {:foo=>0}

Parameters:

  • block (Proc)

    receives a transformed key and value

Returns:

  • (Enumerator, Tash)


940
941
942
943
944
# File 'lib/tash.rb', line 940

def reject(&block)
  return to_enum(:reject) unless block

  new_from_self(@ir.reject(&block))
end

#reject!(&block) ⇒ Enumerator, self or nil

Returns self, whose remaining entries are those for which the block returns false or nil. Returns nil if no entries are removed.

Examples:

Without a block

t = Tash[foo: 0, bar: 1, baz: 2]
e = t.reject! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject!>
t1 = e.each { |key, value| key.start_with?('b') } # => {:foo=>0}

With a block

t = Tash[foo: 0, bar: 1, baz: 2]
t.reject! { |key, value| value < 2 } # => {:foo=>0, :bar=>1}

Parameters:

  • block (Proc)

    receives a transformed key and value

Returns:

  • (Enumerator, self or nil)


961
962
963
964
965
# File 'lib/tash.rb', line 961

def reject!(&block)
  return to_enum(:reject!) unless block

  self if @ir.reject!(&block)
end

#replace(other) ⇒ self

Replaces the entire contents of self with the contents of other. If other is a tash it also replaces the transform block.

Examples:

With a hash

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.replace({Bat: 3, Bam: 4}) # => {:bat=>3, :bam=>4}

With a tash

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.replace(Tash[Bat: 3, Bam: 4, &:upcase]) # => {:BAT=>3, :BAM=>4}

Parameters:

  • other (Tash, Hash)

Returns:

  • (self)


981
982
983
984
985
986
987
988
989
990
# File 'lib/tash.rb', line 981

def replace(other)
  if other.is_a?(self.class)
    @ir.replace(other.to_hash)
    @transform = other.transform_proc
  else
    @ir.replace(other.to_hash.transform_keys { |k| transform(k) })
  end

  self
end

#select(&block) ⇒ Enumerator, Tash Also known as: filter

Returns a new Tash object whose entries are those for which the block returns a truthy value. Returns a new Enumerator if no block given.

Examples:

Without a block

t = Tash[foo: 0, bar: 1, baz: 2]
e = t.select # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select>
e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1}

With a block

t = Tash[foo: 0, bar: 1, baz: 2]
t.select { |key, value| value < 2 } # => {:foo=>0, :bar=>1}

Parameters:

  • block (Proc)

    receives a transformed key and value

Returns:

  • (Enumerator, Tash)


1007
1008
1009
1010
1011
# File 'lib/tash.rb', line 1007

def select(&block)
  return to_enum(:select) unless block

  new_from_self(@ir.select(&block))
end

#select!(&block) ⇒ Enumerator, self or nil Also known as: filter!

Returns self, whose entries are those for which the block returns a truthy value. When given a block, it returns nil if no entries are removed.

Examples:

Without a block

t = Tash[foo: 0, bar: 1, baz: 2]
e = t.select! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:select!>
e.each { |key, value| value < 2 } # => {:foo=>0, :bar=>1}

With a block

t = Tash[foo: 0, bar: 1, baz: 2]
t.select! { |key, value| value < 2 } # => {:foo=>0, :bar=>1}

Parameters:

  • block (Proc)

    receives a transformed key and value

Returns:

  • (Enumerator, self or nil)


1029
1030
1031
1032
1033
# File 'lib/tash.rb', line 1029

def select!(&block)
  return to_enum(:select!) unless block

  self if @ir.select!(&block)
end

#shift[key, value] or default value

Removes the first tash entry and returns a 2-element Array containing the removed key and value. Returns the default value if the hash is empty.

Examples:

t = Tash[foo: 0, bar: 1, baz: 2]
t.shift # => [:foo, 0]
t # => {:bar=>1, :baz=>2}

Returns:



# File 'lib/tash.rb', line 1036


#sizeInteger Also known as: length

Returns the count of entries in self.

Examples:

Tash[foo: 0, bar: 1, baz: 2].size # => 3

Returns:

  • (Integer)


# File 'lib/tash.rb', line 1047


#slice(*keys) ⇒ Tash

Returns a new Tash object containing the entries for the given transformed keys. Any given keys that are not found are ignored.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.slice(:BAZ, :foo) # => {:baz=>2, :foo=>0}

Parameters:

  • *keys (Array<Object>)

Returns:



1065
1066
1067
# File 'lib/tash.rb', line 1065

def slice(*keys)
  new_from_self(@ir.slice(*keys.map { |k| transform(k) }))
end

#to_aArray

Returns a new Array of 2-element Array objects; each nested Array contains a key-value pair from self.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.to_a # => [[:foo, 0], [:bar, 1], [:baz, 2]]

Returns:

  • (Array)


# File 'lib/tash.rb', line 1069


#to_h(&block) ⇒ Hash

Returns a Hash containing the content of self. When a block is given, returns a Hash object whose content is based on the block; the block should return a 2-element Array object specifying the key-value pair to be included in the returned Array.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
h1 = t.to_h { |key, value| [value, key] }
h1 # => {0=>:foo, 1=>:bar, 2=>:baz}

Parameters:

  • block (Proc)

Returns:

  • (Hash)


1092
1093
1094
1095
1096
1097
1098
# File 'lib/tash.rb', line 1092

def to_h(&block)
  if block
    @ir.to_h(&block)
  else
    to_hash
  end
end

#to_hashHash

Returns tash as a Hash.

Returns:

  • (Hash)


1103
1104
1105
# File 'lib/tash.rb', line 1103

def to_hash
  @ir.dup
end

#to_procProc

Returns a Proc object that maps a key to its value.

Examples:

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
proc = t.to_proc
proc.class # => Proc
proc.call(:foo) # => 0
proc.call(:BAR) # => 1
proc.call(:nosuch) # => nil

Returns:

  • (Proc)


1118
1119
1120
1121
1122
1123
# File 'lib/tash.rb', line 1118

def to_proc
  p = @ir.to_proc
  lambda do |key|
    p.call(transform(key))
  end
end

#transform_procProc or nil

Returns the transform proc for self.

Examples:

t = Tash.new
t.transform_proc # => nil
t = Tash.new(&:to_s)
t.transform_proc.class # => Proc
t.transform_proc.call(:a) # => "a"

Returns:

  • (Proc or nil)


1135
1136
1137
# File 'lib/tash.rb', line 1135

def transform_proc
  @transform
end

#transform_values(&block) ⇒ Tash

Returns a new Tash object; each entry has:

  • A key from self.
  • A value provided by the block.

Examples:

Without block

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
e = t.transform_values # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_values>
t1 = e.each { |value| value * 100 }
t1 # => {:foo=>0, :bar=>100, :baz=>200}

With a block

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t1 = t.transform_values { |value| value * 100 }
t1 # => {:foo=>0, :bar=>100, :baz=>200}

Returns:



1156
1157
1158
1159
1160
# File 'lib/tash.rb', line 1156

def transform_values(&block)
  return to_enum(:transform_values) unless block

  new_from_self(@ir.transform_values(&block))
end

#transform_values!(&block) ⇒ self

Returns self, whose keys are unchanged, and whose values are determined by the given block.

Examples:

Without a block

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
e = t.transform_values! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:transform_values!>
t1 = e.each { |value| value * 100 }
t1 # => {:foo=>0, :bar=>100, :baz=>200}

With a block

t = Tash[Foo: 0, Bar: 1, Baz: 2, &:downcase]
t.transform_values! { |value| value * 100 } # => {:foo=>0, :bar=>100, :baz=>200}

Returns:

  • (self)


1176
1177
1178
1179
1180
# File 'lib/tash.rb', line 1176

def transform_values!(&block)
  return to_enum(:transform_values!) unless block

  self if @ir.transform_values!(&block)
end

#value?Boolean Also known as: has_value?

Returns true if value is a value in self, otherwise false.

Returns:

  • (Boolean)


# File 'lib/tash.rb', line 1182


#valuesArray

Returns a new Array containing all values in self.

Examples:

t = Tash[foo: 0, bar: 1, baz: 2]
t.values # => [0, 1, 2]

Returns:

  • (Array)


# File 'lib/tash.rb', line 1187


#values_at(*keys) ⇒ Array

Returns a new Array containing values for the given transformed keys. The default values are returned for any keys that are not found.

Examples:

t = Tash[foo: 0, bar: 1, baz: 2]
t.values_at(:baz, :FOO) # => [2, 0]

keys not found

t.values_at(:hello, :foo) # => [nil, 0]

Returns:

  • (Array)


1207
1208
1209
# File 'lib/tash.rb', line 1207

def values_at(*keys)
  @ir.values_at(*keys.map { |k| transform(k) })
end