my-sugar 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/my-sugar/version.rb +1 -1
- data/lib/my-sugar.rb +15 -10
- data/spec/all_spec.rb +7 -1
- metadata +1 -1
data/lib/my-sugar/version.rb
CHANGED
data/lib/my-sugar.rb
CHANGED
@@ -11,18 +11,23 @@ end
|
|
11
11
|
|
12
12
|
include MySugar::Stuff # ah..
|
13
13
|
|
14
|
-
require 'active_support/core_ext/module/delegation' # oh..
|
14
|
+
require 'active_support/core_ext/module/delegation' # oh..
|
15
15
|
|
16
16
|
# monkeys out of the zoo!
|
17
17
|
class Module
|
18
18
|
def is *others; include *others end
|
19
19
|
end
|
20
|
-
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
20
|
+
# ..
|
21
|
+
# to get method names I used:
|
22
|
+
# >> p methods = Enumerable.instance_methods.select {|x| Enumerable.instance_method(x).arity == 0 }
|
23
|
+
# no bang(!) versions
|
24
|
+
module Enumerable
|
25
|
+
methods = [:sort, :sort_by, :find_all, :select, :reject, :collect, :map, :flat_map,
|
26
|
+
:collect_concat, :partition, :group_by, :all?, :any?, :one?, :none?, :min,
|
27
|
+
:max, :minmax, :min_by, :max_by, :minmax_by, :take_while, :drop_while]
|
28
|
+
methods.each do |name|
|
29
|
+
define_method name[/^.*[^\?]/]+'_' do |&block|
|
30
|
+
send name, &x(&block)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/all_spec.rb
CHANGED
@@ -3,11 +3,17 @@ require 'my-sugar'
|
|
3
3
|
describe MySugar do
|
4
4
|
|
5
5
|
specify 'light block' do
|
6
|
-
( [1,2,3].map &x{ to_s * 5 + ?! }
|
6
|
+
( [1,2,3].map &x{ to_s * 5 + ?! } ).should == %w[ 11111! 22222! 33333!]
|
7
7
|
( [1,2,3].all? &x{ is_a? Integer } ).should be_true
|
8
8
|
( [1,2,3].any? &x{ is_a? String } ).should be_false
|
9
9
|
end
|
10
10
|
|
11
|
+
specify 'even more light block all? &x{...} ==> all_{...}' do
|
12
|
+
( [1,2,3].map_{ to_s * 5 + ?! } ).should == %w[ 11111! 22222! 33333!]
|
13
|
+
( [1,2,3].all_{ is_a? Integer } ).should be_true
|
14
|
+
( [1,2,3].any_{ is_a? String } ).should be_false
|
15
|
+
end
|
16
|
+
|
11
17
|
specify 'is == include' do
|
12
18
|
module Testable; def test; 'here' end end
|
13
19
|
class Any; is Testable end
|