Class: Symbol

Inherits:
Object
  • Object
show all
Defined in:
lib/callable_symbol/callable_symbol.rb

Overview

Useful extension for &:symbol - syntax to make it possible to pass arguments for method in block

['abc','','','def','ghi'].tap(&:delete.(''))             # ==> ['abc','def','ghi']
[1,2,3].map(&:to_s.(2))                                  # ==> ['1','10','11']
['abc','cdef','xy','z','wwww'].select(&:size.() == 4)    # ==> ['cdef', 'wwww']
['abc','aaA','AaA','z'].count(&:upcase.().succ == 'AAB') # ==> 2
[%w{1 2 3 4 5},%w{6 7 8 9}].map(&:join.().length)         # ==> [5,4]

Instance Method Summary collapse

Instance Method Details

#call(*args, &block) ⇒ Object



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
# File 'lib/callable_symbol/callable_symbol.rb', line 8

def call(*args, &block)
  obj = BasicObject.new.instance_exec(self,args,block) do |meth,params,block| 
    @postprocess_meth = [meth]
    @postprocess_args = [params]
    @postprocess_block = [block]
    self
  end
  
  def obj.to_proc
    # proc/lambda are methods that cannot be used in BasicObject. It's possible to use ->(slf){...} syntax since ruby-1.9.3 p194 but it's too modern and not widely spreaded yet
    ::Proc.new do |slf| 
      @postprocess_meth.zip(@postprocess_args,@postprocess_block).inject(slf) do |result,(call_meth,call_args,call_block)| 
        result.send(call_meth, *call_args,&call_block) 
      end
    end
  end
  
  def obj.method_missing(meth,*args,&block)
    @postprocess_meth << meth
    @postprocess_args << args
    @postprocess_block << block
    self
  end
  
  def obj.==(other)
    method_missing(:==, other)
  end
  
  obj
end