Class: Array

Inherits:
Object show all
Includes:
NRSER::Ext::Tree
Defined in:
lib/nrser/core_ext/array.rb,
lib/nrser/core_ext/array/to_proc.rb

Instance Method Summary collapse

Methods included from NRSER::Ext::Tree

#each_branch, #leaves, #map_branches, #map_leaves, #map_tree

Instance Method Details

#ellipsis(*args) ⇒ Object

Calls NRSER.ellipsis on ‘self`.



22
23
24
# File 'lib/nrser/core_ext/array.rb', line 22

def ellipsis *args
  NRSER.ellipsis self, *args
end

#extract!(&block) ⇒ Object



16
17
18
# File 'lib/nrser/core_ext/array.rb', line 16

def extract! &block
  NRSER.extract_from_array! self, &block
end

#restArray

Returns new array consisting of all elements after the first (which may be none, resulting in an empty array).

Returns:

  • (Array)

    new array consisting of all elements after the first (which may be none, resulting in an empty array).



11
12
13
# File 'lib/nrser/core_ext/array.rb', line 11

def rest
  NRSER.rest self
end

#to_chainer(publicly: true) ⇒ Object



93
94
95
# File 'lib/nrser/core_ext/array.rb', line 93

def to_chainer publicly: true
  NRSER.chainer self, publicly: publicly
end

#to_diggerObject

Deprecated.

Old name for #to_proc.



102
103
104
105
106
107
108
# File 'lib/nrser/core_ext/array.rb', line 102

def to_digger
  NRSER.logger.deprecated \
    method: "#{ self.class.name }##{ __method__ }",
    alternative: "#{ self.class.name }#to_proc"

  to_proc
end

#to_messageNRSER::Message Also known as: to_m

Creates a new NRSER::Message from the array.

Examples:


message = [:fetch, :x].to_message
message.send_to x: 'ex', y: 'why?'
# => 'ex'

Returns:



61
62
63
# File 'lib/nrser/core_ext/array.rb', line 61

def to_message
  NRSER::Message.new *self
end

#to_pairArray

Checks that length is 2 and returns ‘self`.

Returns:

  • (Array)

    Array of length 2.

Raises:

  • (TypeError)

    If length is not 2.



38
39
40
41
42
43
44
45
# File 'lib/nrser/core_ext/array.rb', line 38

def to_pair
  unless length == 2
    raise TypeError,
          "Array is not of length 2: #{ self.inspect }"
  end
  
  self
end

#to_procProc

Returns a lambda that calls accepts a single arg and calls either:

  1. ‘#[self.first]` if this array has only one entry.

  2. ‘#dig( *self )` if this array has more than one entry.

Examples:

list = [{id: 1, name: "Neil"}, {id: 2, name: "Mica"}]
list.assoc_by &[:id]
# =>  {
#       1 => {id: 1, name: "Neil"},
#       2 => {id: 2, name: "Mica"},
#     }

Returns:

  • (Proc)

    Lambda proc that accepts a single argument and calls ‘#[]` or `#dig with this array’s contents as the arguments.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nrser/core_ext/array/to_proc.rb', line 22

def to_proc
  method_name = case count
  when 0
    raise NRSER::CountError.new \
      "Can not create getter proc from empty array",
      value: self,
      expected: '> 0',
      count: count
  when 1
    :[]
  else
    :dig
  end
    
  NRSER::Message.new( method_name, *self ).to_proc
end

#to_sender(publicly: true) ⇒ Proc

Create a Proc that accepts a single ‘receiver` and provides this array’s entries as the arguments to ‘#public_send` (or `#send` if the `publicly` option is `false`).

Equivalent to

to_message.to_proc publicly: boolean

Examples:


[:fetch, :x].sender.call x: 'ex'
# => 'ex'

Parameters:

  • publicly (Boolean) (defaults to: true)

    When ‘true`, uses `#public_send` in liu of `#send`.

Returns:

  • (Proc)


86
87
88
# File 'lib/nrser/core_ext/array.rb', line 86

def to_sender publicly: true
  to_message.to_proc publicly: publicly
end