Module: JSI::Arraylike

Included in:
PathedArrayNode
Defined in:
lib/jsi/typelike_modules.rb

Overview

a module of methods for objects which behave like Array but are not Array.

this module is intended to be internal to JSI. no guarantees or API promises are made for non-JSI classes including this module.

Constant Summary collapse

SAFE_INDEX_ONLY_METHODS =

methods which do not need to access the element.

%w(each_index empty? length size)
SAFE_INDEX_ELEMENT_METHODS =

there are some ambiguous ones that are omitted, like #sort, #map / #collect.

%w(| & * + - <=> abbrev at bsearch bsearch_index combination compact count cycle dig drop drop_while fetch find_index first include? index join last pack permutation product reject repeated_combination repeated_permutation reverse reverse_each rindex rotate sample select shelljoin shuffle slice sort take take_while transpose uniq values_at zip)
DESTRUCTIVE_METHODS =
%w(<< clear collect! compact! concat delete delete_at delete_if fill flatten! insert keep_if map! pop push reject! replace reverse! rotate! select! shift shuffle! slice! sort! sort_by! uniq! unshift)
SAFE_METHODS =
SAFE_INDEX_ONLY_METHODS | SAFE_INDEX_ELEMENT_METHODS

Instance Method Summary collapse

Instance Method Details

#assoc(obj) ⇒ Object


215
216
217
218
219
# File 'lib/jsi/typelike_modules.rb', line 215

def assoc(obj)
  # note: assoc implemented here (instead of delegated) due to inconsistencies in whether
  # other implementations expect each element to be an Array or to respond to #to_ary
  detect { |e| e.respond_to?(:to_ary) and e[0] == obj }
end

#inspectString Also known as: to_s

basically the same #inspect as Array, but has the class name and, if responsive, self's #jsi_object_group_text

Returns:

  • (String)

231
232
233
234
# File 'lib/jsi/typelike_modules.rb', line 231

def inspect
  object_group_str = (respond_to?(:jsi_object_group_text) ? jsi_object_group_text : [self.class]).join(' ')
  "\#[<#{object_group_str}>#{self.map { |e| ' ' + e.inspect }.join(',')}]"
end

#pretty_print(q) ⇒ void

This method returns an undefined value.

pretty-prints a representation of this arraylike to the given printer


240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/jsi/typelike_modules.rb', line 240

def pretty_print(q)
  object_group_str = (respond_to?(:jsi_object_group_text) ? jsi_object_group_text : [self.class]).join(' ')
  q.text "\#[<#{object_group_str}>"
  q.group_sub {
    q.nest(2) {
      q.breakable(empty? ? '' : ' ')
      q.seplist(self, nil, :each) { |e|
        q.pp e
      }
    }
  }
  q.breakable ''
  q.text ']'
end

#rassoc(obj) ⇒ Object


222
223
224
225
226
# File 'lib/jsi/typelike_modules.rb', line 222

def rassoc(obj)
  # note: rassoc implemented here (instead of delegated) due to inconsistencies in whether
  # other implementations expect each element to be an Array or to respond to #to_ary
  detect { |e| e.respond_to?(:to_ary) and e[1] == obj }
end