Module: JSI::Util::Arraylike
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
-
#assoc(obj) ⇒ Object
see Array#assoc.
-
#inspect ⇒ String
basically the same #inspect as Array, but has the class name and, if responsive, self's #jsi_object_group_text.
-
#pretty_print(q)
pretty-prints a representation of this arraylike to the given printer.
-
#rassoc(obj) ⇒ Object
see Array#rassoc.
- #to_s ⇒ Object
Instance Method Details
#assoc(obj) ⇒ Object
see Array#assoc
189 190 191 192 193 |
# File 'lib/jsi/util/typelike.rb', line 189 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 |
#inspect ⇒ String
basically the same #inspect as Array, but has the class name and, if responsive, self's #jsi_object_group_text
205 206 207 208 |
# File 'lib/jsi/util/typelike.rb', line 205 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
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/jsi/util/typelike.rb', line 216 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(2) { q.breakable ' ' if !empty? q.seplist(self, nil, :each) { |e| q.pp e } } q.breakable '' if !empty? q.text ']' end |
#rassoc(obj) ⇒ Object
see Array#rassoc
196 197 198 199 200 |
# File 'lib/jsi/util/typelike.rb', line 196 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 |
#to_s ⇒ Object
210 211 212 |
# File 'lib/jsi/util/typelike.rb', line 210 def to_s inspect end |