give4each 0.0.3 → 0.1.0

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.
@@ -3,43 +3,32 @@
3
3
 
4
4
  https://github.com/pasberth/give4each
5
5
 
6
- == install
6
+ == installation
7
7
 
8
8
  $ gem install give4each
9
9
 
10
10
  == description
11
11
 
12
- (1..5).map { |i| i ** 2 } (1..5).map &:**.with(2) とか書けるようにする
12
+ Can write the oneliner as block like the Symbol#to_proc
13
13
 
14
- == usage
14
+ == examples
15
15
 
16
- (1..5).map { |i| i ** 2 }
16
+ require 'give4each'
17
17
 
18
- だからこう書けるようにした
19
-
18
+ # (1..5).map { |i| i ** 2 }
20
19
  (1..5).map &:**.with(2) # => [1, 4, 9, 16, 25]
21
20
 
22
- ああああああ
23
- こういうのももう嫌だ!!!
24
-
25
- def initialize
26
- @stack = []
27
- end
28
-
29
- def foo *xs
30
- xs.each { |x| @stack.push x }
31
- end
32
-
33
- だからこう書けるように(ry
34
-
35
- xs.each &:push.to(@array)
36
-
37
-
38
- こういうのも(ry
21
+ # %w[c++ lisp].map { |lang| (lang + "er").upcase }
22
+ %w[c++ lisp].map &:upcase.of(:+, "er") # => ["C++ER", "LISPER"]
39
23
 
40
- f = "hello %s world"
41
- %w[ruby python].map { |a| f % a }
24
+ # %w[c++ lisp].map { |lang| lang.upcase + "er" }
25
+ %w[c++ lisp].map &:upcase.and(:+, "er") # => ["C++er", "LISPer"]
42
26
 
43
- だからこう(ry
27
+ # stack = []
28
+ # (1..5).each { |item| stack.push item }
29
+ stack = []
30
+ (1..5).each &:push.to(stack)
31
+ stack # => [1, 2, 3, 4, 5]
44
32
 
45
- %w[ruby python].map &:%.in("hello %s world")
33
+ # %w[ruby python].map { |lang| "hello %s world" % lang }
34
+ %w[ruby python].map &:%.in("hello %s world") # => ["hello ruby world", "hello python world"]
data/Rakefile CHANGED
@@ -1,9 +0,0 @@
1
- require './lib/give4each'
2
- require 'hoe'
3
-
4
- Hoe.spec 'give4each' do
5
- self.developer 'pasberth', 'pasberth@gmail.com'
6
- self.rubyforge_name = self.name
7
- self.readme_file = 'README.rdoc'
8
- self.version = Give4Each::VERSION
9
- end
@@ -1,20 +1,20 @@
1
- begin
2
- require 'give4each'
3
- rescue LoadError
4
- require './' + File.dirname(__FILE__) + '/../lib/give4each'
5
- end
1
+ require 'rubygems'
2
+ require 'give4each'
6
3
 
7
- # (1..5).map do |i|
8
- # i ** 2
9
- # end
10
- p (1..5).map &:**.with(2) # => [1, 4, 9, 16, 25]
11
-
12
- # %w[c++ lisp].map do |a|
13
- # a.concat("er").upcase
14
- # end
15
- p %w[c++ lisp].map &:upcase.of_concat.with("er") # => ["C++ER", "LISPER"]
4
+ # (1..5).map { |i| i ** 2 }
5
+ (1..5).map &:**.with(2) # => [1, 4, 9, 16, 25]
16
6
 
17
- # %w[c++ lisp].map do |a|
18
- # a.upcase.concat("er")
19
- # end
20
- p %w[c++ lisp].map &:upcase.and_concat.with("er") # => ["C++er", "LISPer"]
7
+ # %w[c++ lisp].map { |lang| (lang + "er").upcase }
8
+ %w[c++ lisp].map &:upcase.of(:+, "er") # => ["C++ER", "LISPER"]
9
+
10
+ # %w[c++ lisp].map { |lang| lang.upcase + "er" }
11
+ %w[c++ lisp].map &:upcase.and(:+, "er") # => ["C++er", "LISPer"]
12
+
13
+ # stack = []
14
+ # (1..5).each { |item| stack.push item }
15
+ stack = []
16
+ (1..5).each &:push.to(stack)
17
+ stack # => [1, 2, 3, 4, 5]
18
+
19
+ # %w[ruby python].map { |lang| "hello %s world" % lang }
20
+ %w[ruby python].map &:%.in("hello %s world") # => ["hello ruby world", "hello python world"]
@@ -1,7 +1,4 @@
1
- $:.unshift File.dirname(__FILE__)
2
-
3
1
  module Give4Each
4
- VERSION = "0.0.3"
5
2
  end
6
3
 
7
4
  require "give4each/private_helpers"
@@ -9,17 +9,17 @@ class Give4Each::MethodChain
9
9
  # Give4Eeach::MethodChain.new :any, *args, &block
10
10
  # as the:
11
11
  # :any.with *args, &block
12
- def initialize method, *args, &block
12
+ def initialize method, *args, &block # :nodoc:
13
13
  raise TypeError, "#{self.class} need to the symbol of the method." unless method.respond_to? :to_sym
14
14
  @current = natural method, *args, &block
15
15
  @callings = [@current]
16
16
  end
17
-
17
+
18
18
  # Examples::
19
19
  # *of_\**:
20
- # %w[c++ lisp].map &:upcase.of_concat.with("er") # => ["C++ER", "LISPER"]
20
+ # %w[c++ lisp].map &:upcase.of_concat("er") # => ["C++ER", "LISPER"]
21
21
  # *and_\**:
22
- # %w[c++ lisp].map &:upcase.and_concat.with("er") # => ["C++er", "LISPer"]
22
+ # %w[c++ lisp].map &:upcase.and_concat("er") # => ["C++er", "LISPer"]
23
23
  # You can do the same as +with+ if you pass the +args+.
24
24
  # %w[c++ lisp].map &:upcase.and_concat("er") # => ["C++er", "LISPer"]
25
25
  def method_missing method, *args, &block
@@ -35,6 +35,10 @@ class Give4Each::MethodChain
35
35
  super
36
36
  end
37
37
 
38
+ def respond_to? f
39
+ super or Proc.instance_methods.include? f.to_sym
40
+ end
41
+
38
42
  def natural method, *args, &block
39
43
  HasArgs.new method.to_sym, args, block, lambda { |o, has| o.send has.method, *has.args, &has.block }
40
44
  end
@@ -51,21 +55,23 @@ class Give4Each::MethodChain
51
55
  self
52
56
  end
53
57
 
54
- # Wrong :( %w[c++ lisp].map &:upcase.of_+("er")
55
- # Right :) %w[c++ lisp].map &:upcase.of(:+, "er")
58
+ # Wrong :(
59
+ # %w[c++ lisp].map &:upcase.of_+("er")
60
+ # Right :)
61
+ # %w[c++ lisp].map &:upcase.of(:+, "er")
56
62
  def and method, *args, &block
57
63
  @current = natural method, *args, &block
58
64
  @callings.push @current
59
65
  return self
60
66
  end
61
67
 
68
+ # For example, I expect the nil is replaced by 0:
69
+ #
62
70
  # [
63
71
  # [1, 2],
64
72
  # [3],
65
73
  # []
66
- # ].map &:first # => [1, 2, nil]
67
- #
68
- # I expect the nil is replaced by 0.
74
+ # ].map &:first # => [1, 3, nil]
69
75
  #
70
76
  # But this is needlessly long!:
71
77
  #
@@ -73,7 +79,7 @@ class Give4Each::MethodChain
73
79
  # [1, 2],
74
80
  # [3],
75
81
  # []
76
- # ].map { |a| a.first or 0 }
82
+ # ].map { |a| a.first or 0 } # => [1, 3, 0]
77
83
  #
78
84
  # I think I write:
79
85
  #
@@ -81,7 +87,7 @@ class Give4Each::MethodChain
81
87
  # [1, 2],
82
88
  # [3],
83
89
  # []
84
- # ].map &:first.or(0)
90
+ # ].map &:first.or(0) # => [1, 3, 0]
85
91
  #
86
92
  def or default_value
87
93
  old = @current.callback
@@ -91,7 +97,7 @@ class Give4Each::MethodChain
91
97
  self
92
98
  end
93
99
 
94
- # example:
100
+ # *example*:
95
101
  # # (1..5).map do |i|
96
102
  # # i ** 2
97
103
  # # end
@@ -106,6 +112,18 @@ class Give4Each::MethodChain
106
112
  # # a.upcase.concat("er")
107
113
  # # end
108
114
  # p %w[c++ lisp].map &:upcase.and_concat.with("er") # => ["C++er", "LISPer"]
115
+ #
116
+ # the 'a', 'an', and 'the' are aliases for this.
117
+ #
118
+ # This is strange in English, is not you?
119
+ #
120
+ # char = 'l'
121
+ # %w[hello world].map &:count.with(char) # => [2, 1]
122
+ #
123
+ # If you want to use, let's choose what you like.
124
+ #
125
+ # %w[hello world].map &:count.a(char)
126
+ # %w[hello world].map &:count.the('l')
109
127
  def with *args, &block
110
128
  @current.args = args
111
129
  @current.block = block
@@ -133,8 +151,10 @@ class Give4Each::MethodChain
133
151
  # *example*:
134
152
  # receiver = "hello %s world"
135
153
  # %w[ruby python].map &:%.in(receiver) # => ["hello ruby world", "hello python world"]
154
+ #
136
155
  # *method chain*:
137
156
  # %w[ruby python].map &:%.in(receiver).and_upcase # => ["HELLO RUBY WORLD", "HELLO PYTHON WORLD"]
157
+ #
138
158
  # You should not use #to for that.
139
159
  # receiver = "hello %s world"
140
160
  # %w[ruby python].map &:%.to(receiver) # => ["ruby", "python"]
@@ -144,13 +164,9 @@ class Give4Each::MethodChain
144
164
  end
145
165
  self
146
166
  end
147
-
148
- def to_sym
149
- @method
150
- end
151
167
 
152
168
  def to_proc
153
- lambda do |o, &b|
169
+ lambda do |o|
154
170
  @callings.inject o do |o, has|
155
171
  has.callback.call o, has
156
172
  end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ describe "when using for Enumerable" do
4
+
5
+ let(:langs) { %w[c++ lisp] }
6
+ let(:suffix) { "er" }
7
+
8
+ describe "Symbol#of" do
9
+
10
+ context "langs.map &:upcase.of(:+, suffix)" do
11
+ let(:result) { langs.map &:upcase.of(:+, suffix) }
12
+ let(:expected_result) { langs.map { |lang| (lang + suffix).upcase } }
13
+ subject { result }
14
+ it { should == expected_result }
15
+ end
16
+
17
+ context "langs.map &:upcase.of(:+, suffix).and(:+, 's')" do
18
+ let(:result) { langs.map &:upcase.of(:+, suffix).and(:+, 's') }
19
+ let(:expected_result) { langs.map { |lang| (lang + suffix).upcase + 's' } }
20
+ subject { result }
21
+ it { should == expected_result }
22
+ end
23
+ end
24
+
25
+ describe "Symbol#and" do
26
+
27
+ context "langs.map &:upcase.and(:+, suffix)" do
28
+ let(:result) { langs.map &:upcase.and(:+, suffix) }
29
+ let(:expected_result) { langs.map { |lang| lang.upcase + suffix } }
30
+ subject { result }
31
+ it { should == expected_result }
32
+ end
33
+ end
34
+
35
+ describe "Symbol#to" do
36
+
37
+ context "langs.map &:push.to(*receivers)" do
38
+ let(:receivers) { Array.new(3) { [] } }
39
+ let(:destructed_receivers) { Array.new(3) { langs } }
40
+ let(:result) { langs.map &:push.to(*receivers) }
41
+ let(:expected_result) { langs }
42
+ before { result }
43
+
44
+ describe "receivers" do
45
+ subject { receivers }
46
+ it { should == destructed_receivers }
47
+ end
48
+
49
+ describe "result" do
50
+ subject { result }
51
+ it { should == expected_result }
52
+ end
53
+ end
54
+
55
+ context "langs.map &:capitalize.and_push.to(*receivers)" do
56
+ let(:receivers) { Array.new(3) { [] } }
57
+ let(:destructed_receivers) { Array.new(3) { langs.map { |lang| lang.capitalize } } }
58
+ let(:result) { langs.map &:capitalize.and_push.to(*receivers) }
59
+ let(:expected_result) { langs.map &:capitalize }
60
+ before { result }
61
+
62
+ describe "receivers" do
63
+ subject { receivers }
64
+ it { should == destructed_receivers }
65
+ end
66
+
67
+ describe "result" do
68
+ subject { result }
69
+ it { should == expected_result }
70
+ end
71
+ end
72
+
73
+ context "langs.map &:capitalize.of_push.to(*receivers)" do
74
+ let(:receivers) { Array.new(3) { [] } }
75
+ let(:destructed_receivers) { Array.new(3) { langs } }
76
+ let(:result) { langs.map &:capitalize.of_push.to(*receivers) }
77
+ let(:expected_result) { langs.map &:capitalize }
78
+ before { result }
79
+
80
+ describe "receivers" do
81
+ subject { receivers }
82
+ it { should == destructed_receivers }
83
+ end
84
+
85
+ describe "result" do
86
+ subject { result }
87
+ it { should == expected_result }
88
+ end
89
+ end
90
+ end
91
+
92
+ describe "Symbol#in" do
93
+
94
+ context "langs.map &:+.in(receiver)" do
95
+ let(:receiver) { "The " }
96
+ let(:result) { langs.map &:+.in(receiver) }
97
+ let(:expected_result) { langs.map { |lang| receiver + lang } }
98
+ subject { result }
99
+ it { should == expected_result }
100
+ end
101
+
102
+ context "langs.map &:capitalize.and(:+).in(receiver)" do
103
+ let(:receiver) { "The " }
104
+ let(:result) { langs.map &:capitalize.and(:+).in(receiver) }
105
+ let(:expected_result) { langs.map { |lang| receiver + lang.capitalize } }
106
+ subject { result }
107
+ it { should == expected_result }
108
+ end
109
+
110
+ context "langs.map &:capitalize.of(:+).in(receiver)" do
111
+ let(:receiver) { "The " }
112
+ let(:result) { langs.map &:capitalize.of(:+).in(receiver) }
113
+ let(:expected_result) { langs.map { |lang| (receiver + lang).capitalize } }
114
+ subject { result }
115
+ it { should == expected_result }
116
+ end
117
+ end
118
+
119
+ describe "Symbol#or" do
120
+
121
+ context "(0..5).map &:at.in(langs).or(default_value)" do
122
+ let(:default_value) { "none" }
123
+ let(:result) { (0..5).map &:at.in(langs).or(default_value) }
124
+ let(:expected_result) { (0..5).map { |i| langs.at(i) or default_value } }
125
+ subject { result }
126
+ it { should == expected_result }
127
+ end
128
+
129
+ context "(0..5).map &:at.in(langs).or(default_value).and_capitalize" do
130
+ let(:default_value) { "none" }
131
+ let(:result) { (0..5).map &:at.in(langs).or(default_value).and_capitalize }
132
+ let(:expected_result) { (0..5).map { |i| (langs.at(i) or default_value).capitalize } }
133
+ subject { result }
134
+ it { should == expected_result }
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Give4Each::MethodChain do
4
+
5
+ let(:to_s_method) { Give4Each::MethodChain.new :to_s }
6
+ subject { to_s_method }
7
+
8
+ # # Maybe this is passed each time.
9
+ # Proc.instance_methods.each do |proc_method|
10
+ # it { should respond_to proc_method }
11
+ # end
12
+
13
+ describe "#to_proc" do
14
+ let(:receiver) { "hello world" }
15
+ let(:result) { to_s_method.to_proc.call receiver }
16
+ subject { result }
17
+ it { should == "hello world" }
18
+ end
19
+
20
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/../lib/give4each"
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe Symbol do
4
+
5
+ describe "#to_proc" do
6
+ let(:receiver) { Array }
7
+ let(:args) { [2] }
8
+ let(:result) { :new.to_proc.call(receiver, *args) }
9
+ subject { result }
10
+
11
+ it { should == [nil, nil] }
12
+ end
13
+
14
+ describe "#of" do
15
+ let(:receiver) { "ruby" }
16
+ let(:args) { ["ist"] }
17
+ let(:result) { :upcase.of(:+, *args).to_proc.call(receiver) }
18
+ subject { result }
19
+ it { should == "RUBYIST" }
20
+ end
21
+
22
+ describe "#and" do
23
+ let(:receiver) { "ruby" }
24
+ let(:args) { ["ist"] }
25
+ let(:result) { :upcase.and(:+, *args).to_proc.call(receiver) }
26
+ subject { result }
27
+ it { should == "RUBYist" }
28
+ end
29
+
30
+ describe "#of_*" do
31
+ let(:receiver) { "ruby" }
32
+ let(:args) { ["ist"] }
33
+ let(:result) { :upcase.of_concat(*args).to_proc.call(receiver) }
34
+ subject { result }
35
+ it { should == "RUBYIST" }
36
+ end
37
+
38
+ describe "#and_*" do
39
+ let(:receiver) { "ruby" }
40
+ let(:args) { ["ist"] }
41
+ let(:result) { :upcase.and_concat(*args).to_proc.call(receiver) }
42
+ subject { result }
43
+ it { should == "RUBYist" }
44
+ end
45
+
46
+ describe "#with" do
47
+ let(:receiver) { Array }
48
+ let(:args) { [2] }
49
+ let(:result) { :new.with(*args).to_proc.call(receiver) }
50
+ subject { result }
51
+ it { should == [nil, nil] }
52
+ end
53
+
54
+ describe "#to" do
55
+ let(:receiver) { ["hello"] }
56
+ let(:args) { [["world"]] }
57
+ let(:result) { :concat.to(receiver).to_proc.call(*args) }
58
+ before { result }
59
+
60
+ describe "receiver" do
61
+ subject { receiver }
62
+ it { should == ["hello", "world"] }
63
+ end
64
+
65
+ describe "result" do
66
+ subject { result }
67
+ it { should == ["world"] }
68
+ end
69
+ end
70
+
71
+ describe "#in" do
72
+ let(:receiver) { ["hello"] }
73
+ let(:args) { [["world"]] }
74
+ let(:result) { :concat.in(receiver).to_proc.call(*args) }
75
+ before { result }
76
+
77
+ describe "receiver" do
78
+ subject { receiver }
79
+ it { should == ["hello", "world"] }
80
+ end
81
+
82
+ describe "result" do
83
+ subject { result }
84
+ it { should == ["hello", "world"] }
85
+ end
86
+ end
87
+
88
+ end
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.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,56 +9,53 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-24 00:00:00.000000000 Z
12
+ date: 2012-02-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rdoc
16
- requirement: &70353993100060 !ruby/object:Gem::Requirement
15
+ name: rake
16
+ requirement: &70142495229960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: '3.10'
21
+ version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70353993100060
24
+ version_requirements: *70142495229960
25
25
  - !ruby/object:Gem::Dependency
26
- name: hoe
27
- requirement: &70353993099640 !ruby/object:Gem::Requirement
26
+ name: rspec
27
+ requirement: &70142495229080 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
- - - ~>
30
+ - - ! '>='
31
31
  - !ruby/object:Gem::Version
32
- version: '2.13'
32
+ version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70353993099640
36
- description: (1..5).map { |i| i ** 2 } (1..5).map &:**.with(2) とか書けるようにする
37
- email:
38
- - pasberth@gmail.com
35
+ version_requirements: *70142495229080
36
+ description: Can write the oneliner as block like the Symbol#to_proc
37
+ email: pasberth@gmail.com
39
38
  executables: []
40
39
  extensions: []
41
40
  extra_rdoc_files:
42
- - History.txt
41
+ - README.rdoc
43
42
  files:
43
+ - README.rdoc
44
+ - Rakefile
45
+ - examples/example.rb
44
46
  - lib/give4each.rb
45
47
  - lib/give4each/core_ext.rb
46
48
  - lib/give4each/method_chain.rb
47
49
  - lib/give4each/private_helpers.rb
48
- - History.txt
49
- - README.rdoc
50
- - Rakefile
51
- - examples/and.rb
52
- - examples/example.rb
53
- - examples/of.rb
54
- - examples/with.rb
55
- - .gemtest
56
- homepage: https://github.com/pasberth/give4each
50
+ - spec/enumerable_spec.rb
51
+ - spec/give4each/method_chain_spec.rb
52
+ - spec/spec_helper.rb
53
+ - spec/symbol_spec.rb
54
+ homepage: http://github.com/pasberth/give4each
57
55
  licenses: []
58
56
  post_install_message:
59
57
  rdoc_options:
60
- - --main
61
- - README.rdoc
58
+ - --charset=UTF-8
62
59
  require_paths:
63
60
  - lib
64
61
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -74,9 +71,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
71
  - !ruby/object:Gem::Version
75
72
  version: '0'
76
73
  requirements: []
77
- rubyforge_project: give4each
74
+ rubyforge_project:
78
75
  rubygems_version: 1.8.10
79
76
  signing_key:
80
77
  specification_version: 3
81
- summary: (1..5).map { |i| i ** 2 } (1..5).map &:**.with(2) とか書けるようにする
82
- test_files: []
78
+ summary: Can write the oneliner as block like the Symbol#to_proc
79
+ test_files:
80
+ - spec/enumerable_spec.rb
81
+ - spec/give4each/method_chain_spec.rb
82
+ - spec/spec_helper.rb
83
+ - spec/symbol_spec.rb
84
+ has_rdoc:
data/.gemtest DELETED
File without changes
@@ -1,4 +0,0 @@
1
- === 0.0.3 / 2012-2-24
2
- * added Symbol#or
3
- * added Symbol#and
4
- * added Symbol#of
@@ -1,5 +0,0 @@
1
- require 'give4each'
2
- # %w[c++ lisp].map do |a|
3
- # a.upcase.concat("er")
4
- # end
5
- %w[c++ lisp].map &:upcase.and_concat.with("er") # => ["C++er", "LISPer"]
@@ -1,6 +0,0 @@
1
- require 'give4each'
2
-
3
- # %w[c++ lisp].map do |a|
4
- # a.concat("er").upcase
5
- # end
6
- %w[c++ lisp].map &:upcase.of_concat.with("er") # => ["C++ER", "LISPER"]
@@ -1,5 +0,0 @@
1
- require 'give4each'
2
- # (1..5).map do |i|
3
- # i ** 2
4
- # end
5
- (1..5).map &:**.with(2) # => [1, 4, 9, 16, 25]