give4each 0.0.2 → 0.0.3
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.
- data/History.txt +4 -2
- data/README.rdoc +3 -2
- data/examples/and.rb +1 -1
- data/lib/give4each/method_chain.rb +53 -7
- data/lib/give4each/private_helpers.rb +1 -1
- data/lib/give4each.rb +1 -1
- metadata +8 -17
data/History.txt
CHANGED
data/README.rdoc
CHANGED
data/examples/and.rb
CHANGED
@@ -25,13 +25,9 @@ class Give4Each::MethodChain
|
|
25
25
|
def method_missing method, *args, &block
|
26
26
|
case method.to_s
|
27
27
|
when /^of_(.*)$/
|
28
|
-
|
29
|
-
@callings.unshift @current
|
30
|
-
return self
|
28
|
+
return self.of($1, *args, &block)
|
31
29
|
when /^and_(.*)$/
|
32
|
-
|
33
|
-
@callings.push @current
|
34
|
-
return self
|
30
|
+
return self.and($1, *args, &block)
|
35
31
|
end
|
36
32
|
|
37
33
|
return to_proc.send method, *args, &block if Proc.instance_methods.include? method
|
@@ -44,6 +40,56 @@ class Give4Each::MethodChain
|
|
44
40
|
end
|
45
41
|
|
46
42
|
private :natural
|
43
|
+
|
44
|
+
# Wrong :(
|
45
|
+
# %w[c++ lisp].map &:upcase.of_+("er")
|
46
|
+
# Right :)
|
47
|
+
# %w[c++ lisp].map &:upcase.of(:+, "er")
|
48
|
+
def of method, *args, &block
|
49
|
+
@current = natural method, *args, &block
|
50
|
+
@callings.unshift @current
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
# Wrong :( %w[c++ lisp].map &:upcase.of_+("er")
|
55
|
+
# Right :) %w[c++ lisp].map &:upcase.of(:+, "er")
|
56
|
+
def and method, *args, &block
|
57
|
+
@current = natural method, *args, &block
|
58
|
+
@callings.push @current
|
59
|
+
return self
|
60
|
+
end
|
61
|
+
|
62
|
+
# [
|
63
|
+
# [1, 2],
|
64
|
+
# [3],
|
65
|
+
# []
|
66
|
+
# ].map &:first # => [1, 2, nil]
|
67
|
+
#
|
68
|
+
# I expect the nil is replaced by 0.
|
69
|
+
#
|
70
|
+
# But this is needlessly long!:
|
71
|
+
#
|
72
|
+
# [
|
73
|
+
# [1, 2],
|
74
|
+
# [3],
|
75
|
+
# []
|
76
|
+
# ].map { |a| a.first or 0 }
|
77
|
+
#
|
78
|
+
# I think I write:
|
79
|
+
#
|
80
|
+
# [
|
81
|
+
# [1, 2],
|
82
|
+
# [3],
|
83
|
+
# []
|
84
|
+
# ].map &:first.or(0)
|
85
|
+
#
|
86
|
+
def or default_value
|
87
|
+
old = @current.callback
|
88
|
+
@current.callback = lambda do |o, has|
|
89
|
+
old.call o, has or default_value
|
90
|
+
end
|
91
|
+
self
|
92
|
+
end
|
47
93
|
|
48
94
|
# example:
|
49
95
|
# # (1..5).map do |i|
|
@@ -88,7 +134,7 @@ class Give4Each::MethodChain
|
|
88
134
|
# receiver = "hello %s world"
|
89
135
|
# %w[ruby python].map &:%.in(receiver) # => ["hello ruby world", "hello python world"]
|
90
136
|
# *method chain*:
|
91
|
-
# %w[ruby python].map &:%.in(receiver).and_upcase # => ["HELLO RUBY WORLD", "HELLO PYTHON WORLD"]
|
137
|
+
# %w[ruby python].map &:%.in(receiver).and_upcase # => ["HELLO RUBY WORLD", "HELLO PYTHON WORLD"]
|
92
138
|
# You should not use #to for that.
|
93
139
|
# receiver = "hello %s world"
|
94
140
|
# %w[ruby python].map &:%.to(receiver) # => ["ruby", "python"]
|
@@ -3,7 +3,7 @@ module Give4Each; end
|
|
3
3
|
module Give4Each::PrivateHelpers # :nodoc: all
|
4
4
|
|
5
5
|
def allowing_method? f
|
6
|
-
[:with, :a, :an, :the, :to, :in].include? f.to_sym or
|
6
|
+
[:with, :a, :an, :the, :to, :in, :and, :of, :or].include? f.to_sym or
|
7
7
|
[/^of_.*$/, /^and_.*$/].any?(&:=~.with(f.to_s))
|
8
8
|
end
|
9
9
|
|
data/lib/give4each.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: give4each
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rdoc
|
16
|
-
requirement: &
|
16
|
+
requirement: &70353993100060 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.10'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70353993100060
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: hoe
|
27
|
-
requirement: &
|
27
|
+
requirement: &70353993099640 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,13 +32,8 @@ dependencies:
|
|
32
32
|
version: '2.13'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
36
|
-
description:
|
37
|
-
\ \n (1..5).map &:**.with(2) # => [1, 4, 9, 16, 25]\n\nああああああ\nこういうのももう嫌だ!!!\n\n
|
38
|
-
\ def initialize\n @stack = []\n end\n\n def foo *xs\n xs.each { |x| @stack.push
|
39
|
-
x }\n end\n\nだからこう書けるように(ry\n\n xs.each &:push.to(@array)\n\n\nこういうのも(ry\n\n
|
40
|
-
\ f = \"hello %s world\"\n %w[ruby python].map { |a| f % a }\n\nだからこう(ry\n\n %w[ruby
|
41
|
-
python].map &:%.in(\"hello %s world\")"
|
35
|
+
version_requirements: *70353993099640
|
36
|
+
description: (1..5).map { |i| i ** 2 } を (1..5).map &:**.with(2) とか書けるようにする
|
42
37
|
email:
|
43
38
|
- pasberth@gmail.com
|
44
39
|
executables: []
|
@@ -83,9 +78,5 @@ rubyforge_project: give4each
|
|
83
78
|
rubygems_version: 1.8.10
|
84
79
|
signing_key:
|
85
80
|
specification_version: 3
|
86
|
-
summary:
|
87
|
-
&:**.with(2) # => [1, 4, 9, 16, 25] ああああああ こういうのももう嫌だ!!! def initialize @stack
|
88
|
-
= [] end def foo *xs xs.each { |x| @stack.push x } end だからこう書けるように(ry xs.each
|
89
|
-
&:push.to(@array) こういうのも(ry f = "hello %s world" %w[ruby python].map { |a| f
|
90
|
-
% a } だからこう(ry %w[ruby python].map &:%.in("hello %s world")'
|
81
|
+
summary: (1..5).map { |i| i ** 2 } を (1..5).map &:**.with(2) とか書けるようにする
|
91
82
|
test_files: []
|