Module: JSI::Util::Arraylike

Included in:
Base::ArrayNode
Defined in:
lib/jsi/util/typelike.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).map(&:freeze).freeze
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).map(&:freeze).freeze
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).map(&:freeze).freeze
SAFE_METHODS =
SAFE_INDEX_ONLY_METHODS | SAFE_INDEX_ELEMENT_METHODS

Instance Method Summary collapse

Instance Method Details

#assoc(obj) ⇒ Object



185
186
187
188
189
# File 'lib/jsi/util/typelike.rb', line 185

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)


201
202
203
204
# File 'lib/jsi/util/typelike.rb', line 201

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

#pretty_print(q)

This method returns an undefined value.

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



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/jsi/util/typelike.rb', line 210

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

#rassoc(obj) ⇒ Object



192
193
194
195
196
# File 'lib/jsi/util/typelike.rb', line 192

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