Module: JSI::Util::Arraylike Private

Includes:
Enumerable
Included in:
Base::ArrayNode
Defined in:
lib/jsi/util/typelike.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

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 =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

methods which do not need to access the element.

%w(each_index empty? length size).map(&:freeze).freeze
SAFE_INDEX_ELEMENT_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

%w(| & * + - <=> abbrev at bsearch bsearch_index combination compact count cycle difference dig drop drop_while fetch find_index first include? index intersection intersect? 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 union uniq values_at zip).map(&:freeze).freeze
DESTRUCTIVE_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%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 =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

SAFE_INDEX_ONLY_METHODS | SAFE_INDEX_ELEMENT_METHODS

Instance Method Summary collapse

Instance Method Details

#assoc(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

see Array#assoc



154
155
156
157
158
# File 'lib/jsi/util/typelike.rb', line 154

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

#pretty_print(q)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/jsi/util/typelike.rb', line 169

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 {
    q.nest(2) {
      q.breakable ' ' if !empty?
      q.seplist(self) { |e|
        q.pp e
      }
    }
    q.breakable('') if !empty?
  }
  q.text ']'
end

#rassoc(obj) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

see Array#rassoc



161
162
163
164
165
# File 'lib/jsi/util/typelike.rb', line 161

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