Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/array/bbarray.rb,
lib/hash_path/proc.rb,
lib/hash_path/hash_path.rb,
lib/hash_path/path_hash.rb

Overview

Monkey Patches

Instance Method Summary collapse

Instance Method Details

#bridge(*paths) ⇒ Object

Add a hash path to a hash



207
208
209
# File 'lib/hash_path/hash_path.rb', line 207

def bridge(*paths)
  replace(to_tree_hash.bridge(*paths))
end

#diff(ary) ⇒ Object

Displays all elements between this hash and another hash that are different.

Parameters:

  • ary (Array)

    The ary to compare elements to.



66
67
68
# File 'lib/array/bbarray.rb', line 66

def diff(ary)
  (self - ary) + (ary - self)
end

#hash_path(*path) ⇒ Object Also known as: hpath



160
161
162
# File 'lib/hash_path/hash_path.rb', line 160

def hash_path(*path)
  BBLib.hash_path(self, *path)
end

#hash_path_copy(*paths) ⇒ Object Also known as: hpath_copy



168
169
170
# File 'lib/hash_path/hash_path.rb', line 168

def hash_path_copy(*paths)
  BBLib.hash_path_copy(self, *paths)
end

#hash_path_copy_to(to, *paths) ⇒ Object Also known as: hpath_copy_to



172
173
174
# File 'lib/hash_path/hash_path.rb', line 172

def hash_path_copy_to(to, *paths)
  BBLib.hash_path_copy_to(self, to, *paths)
end

#hash_path_delete(*paths) ⇒ Object Also known as: hpath_delete



176
177
178
# File 'lib/hash_path/hash_path.rb', line 176

def hash_path_delete(*paths)
  BBLib.hash_path_delete(self, *paths)
end

#hash_path_for(value) ⇒ Object Also known as: hpath_for



192
193
194
# File 'lib/hash_path/hash_path.rb', line 192

def hash_path_for(value)
  BBLib.hash_path_key_for(self, value)
end

#hash_path_move(*paths) ⇒ Object Also known as: hpath_move



180
181
182
# File 'lib/hash_path/hash_path.rb', line 180

def hash_path_move(*paths)
  BBLib.hash_path_move(self, *paths)
end

#hash_path_move_to(to, *paths) ⇒ Object Also known as: hpath_move_to



184
185
186
# File 'lib/hash_path/hash_path.rb', line 184

def hash_path_move_to(to, *paths)
  BBLib.hash_path_move_to(self, to, *paths)
end

#hash_path_proc(*args) ⇒ Object Also known as: hpath_proc



88
89
90
# File 'lib/hash_path/proc.rb', line 88

def hash_path_proc(*args)
  BBLib::HashPathProc.new(*args).process(self)
end

#hash_path_set(*paths) ⇒ Object Also known as: hpath_set



164
165
166
# File 'lib/hash_path/hash_path.rb', line 164

def hash_path_set(*paths)
  BBLib.hash_path_set(self, *paths)
end

#hash_pathsObject Also known as: hpaths



188
189
190
# File 'lib/hash_path/hash_path.rb', line 188

def hash_paths
  BBLib.hash_path_keys(self)
end

#hmapObject



88
89
90
91
# File 'lib/array/bbarray.rb', line 88

def hmap
  return map unless block_given?
  map { |v| yield(v) }.compact.to_h
end

#interleave(ary) ⇒ Object

Takes two arrays (can be of different length) and interleaves them like [a, b, a, b…]



60
61
62
# File 'lib/array/bbarray.rb', line 60

def interleave(ary)
  BBLib.interleave(self, ary)
end

#join_terms(seperator = :and, delimiter: ', ', encapsulate: nil) ⇒ Object

Conventient way to join an array into a comma seperated list with the last two elements seperated by a word like ‘and’ or ‘or’.

Parameters:

  • seperator (String) (defaults to: :and)

    The term or phrase to seperate the last two elements by

  • delimiter (String) (defaults to: ', ')

    The delimiter used in the join. This allows something other than ‘, ’ to be used

  • encapsulate (String) (defaults to: nil)

    This will optionally encapsulate each element with a character or string. Useful to wrap all elements in quotes.



81
82
83
84
85
86
# File 'lib/array/bbarray.rb', line 81

def join_terms(seperator = :and, delimiter: ', ', encapsulate: nil)
  elements = (encapsulate ? map { |element| element.to_s.encapsulate(encapsulate) } : self)
  return elements.join(delimiter) if size <= 1
  return elements.join(" #{seperator} ") if size == 2
  [elements[0..-2].join(delimiter), elements.last].join(" #{seperator} ")
end

#keys_to_sObject

Converts all keys in nested hashes to strings.



54
55
56
# File 'lib/array/bbarray.rb', line 54

def keys_to_s
  map { |v| v.respond_to?(:keys_to_s) ? v.keys_to_s : v }
end

#keys_to_sym(clean: false) ⇒ Object

Converts all keys in nested hashes to symbols.



49
50
51
# File 'lib/array/bbarray.rb', line 49

def keys_to_sym(clean: false)
  map { |elem| elem.respond_to?(:keys_to_sym) ? elem.keys_to_sym(clean: clean) : elem }
end

#msplit(*delims) ⇒ Object Also known as: multi_split

Splits all elements in an array using a list of delimiters.



42
43
44
# File 'lib/array/bbarray.rb', line 42

def msplit(*delims)
  map { |elem| elem.msplit(*delims) if elem.respond_to?(:msplit) }.flatten
end

#path_hashObject Also known as: phash, _ph



78
79
80
# File 'lib/hash_path/path_hash.rb', line 78

def path_hash
  BBLib.path_hash(self)
end

#to_tree_hashObject

Creates a tree hash wrapper for this array.



71
72
73
# File 'lib/array/bbarray.rb', line 71

def to_tree_hash
  TreeHash.new(self)
end