Module: MultiBlock
- Defined in:
- lib/multi_block.rb
Defined Under Namespace
Class Method Summary collapse
-
.[](*proc_array) ⇒ Object
multiple block transformation method, sorry for the method length and the code dup ;).
Class Method Details
.[](*proc_array) ⇒ Object
multiple block transformation method, sorry for the method length and the code dup ;)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/multi_block.rb', line 7 def self.[](*proc_array) # Create hash representation, proc_array will still get used sometimes proc_hash = {} proc_array.each{ |proc| proc_hash[proc.name] = proc if proc.respond_to?(:name) } # Build yielder proc Proc.new{ |*proc_names_and_args| if proc_names_and_args.empty? # call all procs ret = proc_array.map(&:call) proc_array.size == 1 ? ret.first : ret else proc_names, *proc_args = *proc_names_and_args if proc_names.is_a? Hash # keys: proc_names, values: args proc_names.map{ |proc_name, proc_args| proc = proc_name.is_a?(Integer) ? proc_array[proc_name] : proc_hash[proc_name.to_sym] proc or raise LocalJumpError, "wrong block name given (#{proc_name})" [proc, Array(proc_args)] }.map{ |proc, proc_args| proc.call(*proc_args) } else ret = Array(proc_names).map{ |proc_name| proc = proc_name.is_a?(Integer) ? proc_array[proc_name] : proc_hash[proc_name.to_sym] proc or raise LocalJumpError, "wrong block name given (#{proc_name})" [proc, Array(proc_args)] }.map{ |proc, proc_args| proc.call(*proc_args) } ret.size == 1 ? ret.first : ret end end } end |