mug 0.5.3 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mug/and-or.rb +26 -26
- data/lib/mug/any-and-all.rb +16 -16
- data/lib/mug/apply.rb +37 -37
- data/lib/mug/array/extend.rb +43 -43
- data/lib/mug/array/minus.rb +32 -32
- data/lib/mug/bool.rb +71 -71
- data/lib/mug/clamp.rb +35 -35
- data/lib/mug/counts.rb +27 -27
- data/lib/mug/fragile-method-chain.rb +42 -42
- data/lib/mug/hash/map.rb +75 -75
- data/lib/mug/hash/operations.rb +47 -47
- data/lib/mug/iterator/for.rb +7 -7
- data/lib/mug/iterator/method.rb +7 -7
- data/lib/mug/iterator_c.rb +14 -14
- data/lib/mug/loop-with.rb +30 -29
- data/lib/mug/matchdata/each.rb +40 -40
- data/lib/mug/matchdata/hash.rb +29 -29
- data/lib/mug/maybe.rb +33 -33
- data/lib/mug/negativity.rb +20 -20
- data/lib/mug/not.rb +6 -6
- data/lib/mug/rexproc.rb +6 -6
- data/lib/mug/self.rb +31 -31
- data/lib/mug/to_h.rb +2 -2
- data/lib/mug/top.rb +100 -100
- data/test/test-and-or.rb +32 -32
- data/test/test-any-and-all.rb +19 -19
- data/test/test-apply.rb +47 -47
- data/test/test-array-extend.rb +54 -54
- data/test/test-array-minus.rb +11 -11
- data/test/test-bool.rb +48 -48
- data/test/test-clamp.rb +42 -42
- data/test/test-counts.rb +21 -21
- data/test/test-fragile-method-chain.rb +56 -56
- data/test/test-hashmap.rb +14 -14
- data/test/test-hashop.rb +47 -47
- data/test/test-iterator-for.rb +12 -12
- data/test/test-loop-with.rb +117 -66
- data/test/test-matchdata_each.rb +50 -50
- data/test/test-matchdata_hash.rb +40 -40
- data/test/test-maybe.rb +76 -76
- data/test/test-negativity.rb +40 -40
- data/test/test-not.rb +53 -53
- data/test/test-rexproc.rb +6 -6
- data/test/test-self.rb +21 -21
- data/test/test-tau.rb +12 -12
- data/test/test-top.rb +100 -100
- metadata +4 -4
data/lib/mug/iterator/for.rb
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
require_relative '../iterator_c'
|
3
3
|
|
4
4
|
class Object
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
#
|
6
|
+
# Creates a new Iterator for the method named +meth+
|
7
|
+
#
|
8
|
+
def iter_for meth, *args
|
9
|
+
Iterator.new self, meth, *args
|
10
|
+
end
|
11
|
+
alias :to_iter :iter_for
|
12
12
|
end
|
13
13
|
|
14
14
|
=begin
|
data/lib/mug/iterator/method.rb
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
require_relative '../iterator_c'
|
3
3
|
|
4
4
|
class Method
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
#
|
6
|
+
# Creates a new Iterator for this method, initially invoked
|
7
|
+
# on this method's receiver.
|
8
|
+
#
|
9
|
+
def to_iter *args
|
10
|
+
Iterator.new receiver, name, *args
|
11
|
+
end
|
12
12
|
end
|
13
13
|
|
14
14
|
=begin
|
data/lib/mug/iterator_c.rb
CHANGED
@@ -10,20 +10,20 @@
|
|
10
10
|
# 0.iter_for(:next).take(5) #=> [0,1,2,3,4]
|
11
11
|
#
|
12
12
|
class Iterator < Enumerator
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
13
|
+
#
|
14
|
+
# Creates a new Iterator for method +meth+, to be
|
15
|
+
# called initially on object +obj+.
|
16
|
+
#
|
17
|
+
# All method calls will have +args+ as parameters.
|
18
|
+
#
|
19
|
+
def initialize obj, meth, *args
|
20
|
+
super() do |y|
|
21
|
+
loop do
|
22
|
+
y << obj
|
23
|
+
obj = obj.send(meth, *args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
27
|
end
|
28
28
|
|
29
29
|
=begin
|
data/lib/mug/loop-with.rb
CHANGED
@@ -1,34 +1,35 @@
|
|
1
1
|
|
2
2
|
module Kernel
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
3
|
+
#
|
4
|
+
# Repeatedly executes the block, yielding the current iteration
|
5
|
+
# count, which starts from +offset+. If no block is given, returns
|
6
|
+
# an Enumerator.
|
7
|
+
#
|
8
|
+
def loop_with_index(offset=0)
|
9
|
+
return c=enum_for(:loop_with_index, offset) unless block_given?
|
10
|
+
c = 0 + offset
|
11
|
+
begin
|
12
|
+
while true
|
13
|
+
yield c
|
14
|
+
c += 1
|
15
|
+
end
|
16
|
+
rescue StopIteration
|
17
|
+
end
|
18
|
+
c
|
19
|
+
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
21
|
+
#
|
22
|
+
# Repeatedly executes the block, yielding an arbitrary object, +obj+.
|
23
|
+
#
|
24
|
+
def loop_with_object(obj)
|
25
|
+
return obj=enum_for(:loop_with_object, obj) unless block_given?
|
26
|
+
begin
|
27
|
+
while true
|
28
|
+
yield obj
|
29
|
+
end
|
30
|
+
rescue StopIteration
|
31
|
+
end
|
32
|
+
obj
|
33
|
+
end
|
33
34
|
end
|
34
35
|
|
data/lib/mug/matchdata/each.rb
CHANGED
@@ -1,49 +1,49 @@
|
|
1
1
|
|
2
2
|
class MatchData
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
# Iterates over each capture group in the MatchData object,
|
5
|
+
# including +$&+ (the entire matched string), yielding the
|
6
|
+
# captured string.
|
7
|
+
def each &b
|
8
|
+
return enum_for(:each) unless block_given?
|
9
|
+
to_a.each{|v| yield v }
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
12
|
+
# Iterates over each capture group in the MatchData object,
|
13
|
+
# yielding the capture position and captured string.
|
14
|
+
#
|
15
|
+
# The capture positions are either all Strings or all Integers,
|
16
|
+
# depending on whether the original Regexp had named capture
|
17
|
+
# groups or not.
|
18
|
+
def each_capture &b
|
19
|
+
return enum_for(:each_capture) unless block_given?
|
20
|
+
if names.empty?
|
21
|
+
captures.each_with_index{|v,i| yield i+1, v }
|
22
|
+
else
|
23
|
+
names.each{|n| yield n, self[n] }
|
24
|
+
end
|
25
|
+
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
# Iterates over each named capture group in the MatchData object,
|
28
|
+
# yielding the capture name and string.
|
29
|
+
def each_named_capture &b
|
30
|
+
return enum_for(:each_named_capture) unless block_given?
|
31
|
+
names.each{|n| yield n, self[n] }
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
34
|
+
# Iterates over each positional capture group in the MatchData object,
|
35
|
+
# yielding the capture position and string.
|
36
|
+
#
|
37
|
+
# If +include_names+ is given and true, treats named captures
|
38
|
+
# as positional captures.
|
39
|
+
#
|
40
|
+
# WARNING: if mixing named and positional captures, no positional
|
41
|
+
# captures will be available using this method!
|
42
|
+
def each_positional_capture include_names: false, &b
|
43
|
+
return enum_for(:each_positional_capture, include_names: include_names) unless block_given?
|
44
|
+
return unless names.empty? || include_names
|
45
|
+
captures.each_with_index{|v,i| yield i+1, v }
|
46
|
+
end
|
47
47
|
|
48
48
|
end
|
49
49
|
|
data/lib/mug/matchdata/hash.rb
CHANGED
@@ -1,35 +1,35 @@
|
|
1
1
|
|
2
2
|
class MatchData
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
4
|
+
# Returns a Hash object of capture position => captured string.
|
5
|
+
#
|
6
|
+
# The capture positions are either all Strings or all Integers,
|
7
|
+
# depending on whether the original Regexp had named capture
|
8
|
+
# groups or not.
|
9
|
+
def to_h
|
10
|
+
if names.empty?
|
11
|
+
Hash[ captures.each_with_index.map{|v,i| [i+1, v] } ]
|
12
|
+
else
|
13
|
+
Hash[ names.map{|n| [n, self[n]] } ]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns a Hash object of capture name => captured string.
|
18
|
+
def named_captures
|
19
|
+
Hash[ names.map{|n| [n, self[n]] } ]
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns a Hash object of capture position => captured string.
|
23
|
+
#
|
24
|
+
# If +include_names+ is given and true, treats named captures
|
25
|
+
# as positional captures.
|
26
|
+
#
|
27
|
+
# WARNING: if mixing named and positional captures, no positional
|
28
|
+
# captures will be available using this method!
|
29
|
+
def positional_captures include_names: false
|
30
|
+
return {} unless names.empty? || include_names
|
31
|
+
Hash[ captures.each_with_index.map{|v,i| [i+1, v] } ]
|
32
|
+
end
|
33
33
|
|
34
34
|
end
|
35
35
|
|
data/lib/mug/maybe.rb
CHANGED
@@ -3,42 +3,42 @@
|
|
3
3
|
# Invokes methods on a wrapped object, if that object is truthy.
|
4
4
|
#
|
5
5
|
class MaybeDelegator
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
6
|
+
#
|
7
|
+
# Creates a new MaybeDelegator, wrapping +o+
|
8
|
+
#
|
9
|
+
def initialize o
|
10
|
+
@o = o
|
11
|
+
end
|
12
|
+
|
13
|
+
#
|
14
|
+
# Returns this MaybeDelegator object.
|
15
|
+
#
|
16
|
+
def maybe
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
# Calls the method on +@o+ if it's truthy.
|
21
|
+
def method_missing *a, &b #:nodoc:
|
22
|
+
@o && @o.send(*a, &b)
|
23
|
+
end
|
24
24
|
end
|
25
25
|
|
26
26
|
class Object
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
27
|
+
#
|
28
|
+
# Do something if this object is truthy.
|
29
|
+
#
|
30
|
+
# If a block is given, it is executed in the context of this
|
31
|
+
# object, iff this object is neither +nil+ nor +false+.
|
32
|
+
#
|
33
|
+
# If no block is given, returns a MaybeDelegator object.
|
34
|
+
#
|
35
|
+
def maybe &b
|
36
|
+
if b
|
37
|
+
self && instance_eval(&b)
|
38
|
+
else
|
39
|
+
MaybeDelegator.new(self)
|
40
|
+
end
|
41
|
+
end
|
42
42
|
end
|
43
43
|
|
44
44
|
=begin
|
data/lib/mug/negativity.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
|
2
2
|
class Numeric
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
if RUBY_VERSION < '2.3'
|
5
|
+
def negative?
|
6
|
+
self < 0 ? self : nil
|
7
|
+
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
def positive?
|
10
|
+
self > 0 ? self : nil
|
11
|
+
end
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
def nonnegative?
|
15
|
+
self < 0 ? nil : self
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
def nonpositive?
|
19
|
+
self > 0 ? nil : self
|
20
|
+
end
|
21
21
|
|
22
22
|
end
|
23
23
|
|
24
24
|
class Complex
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
25
|
+
if RUBY_VERSION < '2.3'
|
26
|
+
undef :negative?
|
27
|
+
undef :positive?
|
28
|
+
end
|
29
|
+
undef :nonnegative?
|
30
|
+
undef :nonpositive?
|
31
31
|
end
|
32
32
|
|
33
33
|
=begin
|
data/lib/mug/not.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
|
2
2
|
module Kernel
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
#
|
4
|
+
# Negate a predicate.
|
5
|
+
#
|
6
|
+
def not(*a, &b)
|
7
|
+
not a.empty? ? (b ? (yield self) : self) : __send__(*a, &b)
|
8
|
+
end
|
9
9
|
end
|
10
10
|
|
11
11
|
=begin
|