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)
- 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
-
#assoc(obj) ⇒ Object
see Array#assoc.
-
#inspect ⇒ String
(also: #to_s)
basically the same #inspect as Array, but has the class name and, if responsive, self's #jsi_object_group_text.
-
#pretty_print(q) ⇒ void
pretty-prints a representation of this arraylike to the given printer.
-
#rassoc(obj) ⇒ Object
see Array#rassoc.
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 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
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) ⇒ void
This method returns an undefined value.
pretty-prints a representation of this arraylike to the given printer
214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/jsi/util/typelike.rb', line 214 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(empty? ? '' : ' ') q.seplist(self, nil, :each) { |e| q.pp e } } } q.breakable '' 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 |