Module: JSI::PathedArrayNode

Includes:
Arraylike
Defined in:
lib/jsi/pathed_node.rb

Constant Summary

Constants included from Arraylike

Arraylike::DESTRUCTIVE_METHODS, Arraylike::SAFE_INDEX_ELEMENT_METHODS, Arraylike::SAFE_INDEX_ONLY_METHODS, Arraylike::SAFE_METHODS

Instance Method Summary collapse

Methods included from Arraylike

#assoc, #inspect, #pretty_print, #rassoc

Instance Method Details

#each(*a) {|Object| ... } ⇒ self, Enumerator

yields each array element of this node.

each yielded element is the result of self[index] for each index of our array (see #[]).

returns an Enumerator if no block is given.

Parameters:

  • a

    arguments are passed to #[]

Yields:

  • (Object)

    each element of this array node

Returns:

  • (self, Enumerator)

    an Enumerator if invoked without a block; otherwise self


80
81
82
83
84
# File 'lib/jsi/pathed_node.rb', line 80

def each(*a, &block)
  return to_enum(__method__) { jsi_node_content_ary_pubsend(:size) } unless block
  jsi_node_content_ary_pubsend(:each_index) { |i| yield(self[i, *a]) }
  self
end

#jsi_node_content_ary_pubsend(method_name, *a, &b) ⇒ Object

invokes the method with the given name on the jsi_node_content (if defined) or its #to_ary

Parameters:

  • method_name (String, Symbol)
  • a

    arguments and block are passed to the invocation of method_name

Returns:

  • (Object)

    the result of calling method method_name on the jsi_node_content or its #to_ary


100
101
102
103
104
105
106
# File 'lib/jsi/pathed_node.rb', line 100

def jsi_node_content_ary_pubsend(method_name, *a, &b)
  if jsi_node_content.respond_to?(method_name)
    jsi_node_content.public_send(method_name, *a, &b)
  else
    jsi_node_content.to_ary.public_send(method_name, *a, &b)
  end
end

#to_ary(*a) ⇒ Array

an array, the same size as the jsi_node_content, in which the element at each index is the result of self[index]

Parameters:

  • a

    arguments are passed to #[]

Returns:

  • (Array)

90
91
92
# File 'lib/jsi/pathed_node.rb', line 90

def to_ary(*a)
  to_a(*a)
end