ect

Gem Version

Ruby methods ending in ect.

The subject of last part of a lightning talk for Hiroshima.rb #058.

I like to chain methods ending in ect. I wanted to have more of them.

Mostly a joke.

Object

Object#inflect

inflect |ɪnˈflɛkt|

ORIGIN late Middle English (in sense 3): from Latin inflectere, from in- ‘into’ + flectere ‘to bend’.

An alias to #tap. Passes the instance to the block, returns the instance.

%w[ fox lion crow donkey wolf ]
  .inflect { |a| a << 'mole' }
  .collect(&:capitalize)

Object#deflect

deflect |dɪˈflɛkt|

ORIGIN mid 16th cent.: from Latin deflectere, from de- ‘away from’ + flectere ‘to bend’.

A very simple yield(self) (in Ruby 2.5.x it could thus become an alias to #yield_self).

Passes the instance to the block, returns the result of the block. Change of direction.

[ animals_list_1, animals_list_2 ]
  .deflect { |a, b| a.include?('mole') ? a : b }
  .collect(&:capitalize)
user = DB[:users]
  .select(:id, :name)
  .deflect { |x| id ? x.where(id: id) : x.where(name: name) }
  .first

Enumerable

Enumerable#bisect

bisect |bʌɪˈsɛkt|

ORIGIN mid 17th cent.: from bi-‘two’ + Latin sect- (from secare ‘to cut’).

An alias to #partition. Returns two arrays (the true array and the false array).

%w[ fox lion crow donkey wolf ]
  .bisect { |a| a.length > 3 }
    #
    # => [ %w[ lion crow donkey wolf ],
    #      %w[ fox ] ]

Enumerable#dissect

dissect |dʌɪˈsɛkt, dɪ-|

ORIGIN late 16th cent.: from Latin dissect- ‘cut up’, from the verb dissecare, from dis- ‘apart’ + secare ‘to cut’.

"Cuts" the incoming Enumerable instance in multiple arrays.

a0, a1, a2 = (1..14).dissect { |i| i % 3 }

p a0  # => [ 3, 6, 9, 12 ]
p a1  # => [ 1, 4, 7, 10, 13 ]
p a2  # => [ 2, 5, 8, 11, 14 ]

A sparse answer is possible:

a0, a1, a2 = (1..14)
  .dissect { |i|
    case m = i % 3
    when 0, 2 then m
    else 0
    end }

p a0  # => [ 1, 3, 4, 6, 7, 9, 10, 12, 13 ]
p a1  # => nil
p a2  # => [ 2, 5, 8, 11, 14 ]

Enumerable#elect

elect |əˈlekt|

ORIGIN late Middle English: from Latin elect- ‘picked out’, from the verb eligere, from e- (variant of ex-) ‘out’ + legere ‘to pick’.

Like a find, but yields the result of the block instead of the matching element.

a = [ { a: 1 }, { b: 2 }, { c: 3 } ]

a.elect { |e| e[:b] }  # => 2
a.elect { |e| e[:z] }  # => nil

LICENSE

MIT, see LICENSE.txt