Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/console_utils/core_ext/array_to_proc.rb

Instance Method Summary collapse

Instance Method Details

#to_procObject

:call-seq:

[].to_proc
&[chain, [:of_calls, and, args], ...]

Converts array to proc with chained calls of items. Every item can be either a method name or an array containing a method name and args.

Examples

the_hash = { :one => "One", :two => "Two", :three => 3, :four => nil }
mapping = { "one" => "1", "two" => "2", "" => "0" }

the_hash.select(&[[:[], 1], [:is_a?, String]])
# => { :one => "One", :two => "Two" }

the_hash.values.map(&[:to_s, :downcase, [:sub, /one|two|$^/, mapping]])
# => ["1", "2", "3", "0"]

21
22
23
24
25
26
27
28
# File 'lib/console_utils/core_ext/array_to_proc.rb', line 21

def to_proc
  proc do |*obj|
    obj = obj.shift if obj.size == 1
    reduce(obj) do |chain, sym|
      chain.public_send(*Array(sym).flatten)
    end
  end
end