Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/bblib/core/util/array.rb,
lib/bblib/core/hash_path/hash_path.rb

Overview

Monkey Patches

Instance Method Summary collapse

Instance Method Details

#bridge(*paths) ⇒ Object

Add a hash path to a hash



205
206
207
# File 'lib/bblib/core/hash_path/hash_path.rb', line 205

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/bblib/core/util/array.rb', line 66

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

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



158
159
160
# File 'lib/bblib/core/hash_path/hash_path.rb', line 158

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

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



166
167
168
# File 'lib/bblib/core/hash_path/hash_path.rb', line 166

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



170
171
172
# File 'lib/bblib/core/hash_path/hash_path.rb', line 170

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



174
175
176
# File 'lib/bblib/core/hash_path/hash_path.rb', line 174

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

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



190
191
192
# File 'lib/bblib/core/hash_path/hash_path.rb', line 190

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

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



178
179
180
# File 'lib/bblib/core/hash_path/hash_path.rb', line 178

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



182
183
184
# File 'lib/bblib/core/hash_path/hash_path.rb', line 182

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

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



162
163
164
# File 'lib/bblib/core/hash_path/hash_path.rb', line 162

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

#hash_pathsObject Also known as: hpaths



186
187
188
# File 'lib/bblib/core/hash_path/hash_path.rb', line 186

def hash_paths
  BBLib.hash_path_keys(self)
end

#hmapObject



88
89
90
91
# File 'lib/bblib/core/util/array.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/bblib/core/util/array.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/bblib/core/util/array.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/bblib/core/util/array.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/bblib/core/util/array.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/bblib/core/util/array.rb', line 42

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

#to_tree_hashObject

Creates a tree hash wrapper for this array.



71
72
73
# File 'lib/bblib/core/util/array.rb', line 71

def to_tree_hash
  TreeHash.new(self)
end