Module: Enumerable::Arguments

Defined in:
lib/mixers/enumargs.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wrap_enumerable_method(methodname) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mixers/enumargs.rb', line 50

def self.wrap_enumerable_method( methodname )

  m = methodname
  meth = Enumerable.instance_method(m)
  arity = meth.arity

  case arity <=> 0
  when 0
    class_eval %{
      def #{m}( *args, &yld )
        enum_for(:each, *args).#{m}( &yld )
      end
    }
  when 1
    class_eval %{
      def #{m}( *args, &yld )
        args, each_args = args[0...#{arity}], args[#{arity}..-1]
        enum_for(:each, *each_args).#{m}( *args, &yld )
      end
    }
  else
    class_eval %{
      def #{m}( *args, &yld )
        enum_for(:each).#{m}( *args, &yld )
      end
    }
  end
end

Instance Method Details

#find(*args, &yld) ⇒ Object Also known as: detect

Make exception for #find (a negative arity method) to accept keyword argument.

ObjectSpace.find(Class, :ifnone=>lambda{1}) { |e| ... }
ObjectSpace.find(Class, :ifnone=>lambda{1}) { |e| ... }


94
95
96
97
98
99
100
101
102
# File 'lib/mixers/enumargs.rb', line 94

def find(*args, &yld)  # future use **keys ?
  if Hash === args.last and args.last.key?(:ifnone)
    ifnone = args.last.delete(:ifnone)
    args.pop if args.last.empty?
    enum_for(:each, *args).find( ifnone, &yld )
  else
    enum_for(:each, *args).find( &yld )
  end
end

#to_a(*args) ⇒ Object



84
85
86
# File 'lib/mixers/enumargs.rb', line 84

def to_a(*args)
  map(*args){ |x| x }
end