addy 1.0.0 → 1.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.
data/README.md CHANGED
@@ -33,28 +33,46 @@ Then use it!
33
33
  end
34
34
  end
35
35
 
36
+ When you include addy on a class that implements inject, you don't even need to pass a value to it. Instead it calls sum on your class.
37
+
38
+ require 'addy'
39
+
40
+ class MyClass < Range
41
+ include Addy
42
+
43
+ def my_awesome_adder
44
+ sum
45
+ end
46
+ end
47
+
36
48
  ### Calling It
37
49
  You can call either sum or summation. They're aliases for the same thing.
38
50
 
51
+ Note: The following assumes Addy is included into Range.
52
+
39
53
  When you pass a block to sum it will execute the block on the current number before adding it to the sum.
40
54
 
41
- sum(1..5) {|num| num + 1} #=> 20
55
+ sum(1..5) {|num| num + 1} #=> 20
56
+ (1..5).sum {|num| num + 1} #=> 20
42
57
 
43
58
  You don't have to pass a block though!
44
59
 
45
60
  #this
46
- sum(1..5) #=> 15
61
+ sum(1..5) #=> 15
62
+ #and
63
+ (1..5).sum #=> 15
64
+
65
+ #are equivalent to
66
+ sum(1..5) {|num| num} #=> 15
67
+ #and
68
+ (1..5).sum {|num| num} #=> 15
47
69
 
48
- #is equivalent to
49
- sum(1..5) {|num| num} #=> 15
50
70
  ### Input
51
71
  Ranges and numeric arrays both work well.
52
72
 
53
73
  sum(1..5) #=> 15
54
74
  sum([1,2,3,4,5]) #=> 15
55
75
 
56
-
57
-
58
76
  ## Note on Patches/Pull Requests
59
77
 
60
78
  * Fork the project.
@@ -69,4 +87,4 @@ Ranges and numeric arrays both work well.
69
87
 
70
88
  Copyright (c) 2010 Allen Madsen. See LICENSE for details.
71
89
 
72
- ## PS: Isn't it ridiculous how much documentation I wrote for one function?
90
+ PS: Isn't it ridiculous how much documentation I wrote for one function?
data/Rakefile CHANGED
@@ -1,12 +1,16 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
3
 
4
+ def get_description
5
+ File.read("./README.md")
6
+ end
7
+
4
8
  begin
5
9
  require 'jeweler'
6
10
  Jeweler::Tasks.new do |gem|
7
11
  gem.name = "addy"
8
12
  gem.summary = %Q{Prettier summations in your code.}
9
- gem.description = %Q{Prettier summations in your code.}
13
+ gem.description = get_description
10
14
  gem.email = "blatyo@gmail.com"
11
15
  gem.homepage = "http://github.com/blatyo/addy"
12
16
  gem.authors = ["Allen Madsen"]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -1,6 +1,10 @@
1
1
  module Addy
2
- def summation(range)
3
- range.inject(0) do |memo, num|
2
+ def summation(injectable = nil)
3
+ injectable ||= self
4
+
5
+ raise "#{injectable.class.name} does not implement inject." unless injectable.respond_to? :inject
6
+
7
+ injectable.inject(0) do |memo, num|
4
8
  if block_given?
5
9
  yield(num) + memo
6
10
  else
@@ -1,23 +1,51 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe Addy do
4
- before(:all) do
5
- class ::Object
6
- include ::Addy
7
- end
8
- end
9
-
10
4
  describe "#sum" do
11
- context "when no block is given" do
12
- it "should calculate the sum of the values in the range" do
13
- sum(1..5).should == 15
5
+ before(:all) do
6
+ class NotInjectable
7
+ include ::Addy
14
8
  end
15
9
  end
16
10
 
17
- context "when a block is given" do
18
- it "should calculate the sum of the results of the block for each value in the range" do
11
+ context "when sum is called with a parameter" do
12
+ before(:all) do
13
+ class ::Object
14
+ include ::Addy
15
+ end
16
+ end
17
+
18
+ it "should calculate the sum of the values passed in when no block is given" do
19
+ sum(1..5).should == 15
20
+ end
21
+
22
+ it "should calculate the sum of the results of the block for the values passed in when a block is given" do
19
23
  sum(1..3) {|i| i**i }.should == 32
20
24
  end
25
+
26
+ it "should raise an exception when self does not respond to inject" do
27
+ lambda {sum NotInjectable.new}.should raise_error "NotInjectable does not implement inject."
28
+ end
29
+ end
30
+
31
+ context "when sum is called without a parameter" do
32
+ before(:all) do
33
+ class ::Range
34
+ include ::Addy
35
+ end
36
+ end
37
+
38
+ it "should calculate the sum of the values of self when no block is given" do
39
+ (1..5).sum.should == 15
40
+ end
41
+
42
+ it "should calculate the sum of the results of the block for each value of self when a block is given" do
43
+ (1..3).sum {|i| i**i }.should == 32
44
+ end
45
+
46
+ it "should raise an exception when self does not respond to inject" do
47
+ lambda {NotInjectable.new.sum}.should raise_error "NotInjectable does not implement inject."
48
+ end
21
49
  end
22
50
  end
23
51
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
+ - 1
7
8
  - 0
8
- - 0
9
- version: 1.0.0
9
+ version: 1.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Allen Madsen
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-19 00:00:00 -04:00
17
+ date: 2010-06-28 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -58,7 +58,97 @@ dependencies:
58
58
  version: "0"
59
59
  type: :development
60
60
  version_requirements: *id003
61
- description: Prettier summations in your code.
61
+ description: |-
62
+ # Addy
63
+
64
+ Allows pretty summations. Instead of writing:
65
+
66
+ (1..5).inject(0) do |memo, num|
67
+ memo + (num**num)
68
+ end
69
+
70
+ You write:
71
+
72
+ sum(1..5) do |num|
73
+ num**num
74
+ end
75
+
76
+ Personally, I would rather write the latter.
77
+
78
+ ## Usage
79
+
80
+ Install the gem:
81
+
82
+ gem install addy
83
+
84
+ Then use it!
85
+
86
+ require 'addy'
87
+
88
+ class MyClass
89
+ #include it in a class or in Object to get it everywhere
90
+ include Addy
91
+
92
+ def my_awesome_adder(range)
93
+ sum(range)
94
+ end
95
+ end
96
+
97
+ When you include addy on a class that implements inject, you don't even need to pass a value to it. Instead it calls sum on your class.
98
+
99
+ require 'addy'
100
+
101
+ class MyClass < Range
102
+ include Addy
103
+
104
+ def my_awesome_adder
105
+ sum
106
+ end
107
+ end
108
+
109
+ ### Calling It
110
+ You can call either sum or summation. They're aliases for the same thing.
111
+
112
+ Note: The following assumes Addy is included into Range.
113
+
114
+ When you pass a block to sum it will execute the block on the current number before adding it to the sum.
115
+
116
+ sum(1..5) {|num| num + 1} #=> 20
117
+ (1..5).sum {|num| num + 1} #=> 20
118
+
119
+ You don't have to pass a block though!
120
+
121
+ #this
122
+ sum(1..5) #=> 15
123
+ #and
124
+ (1..5).sum #=> 15
125
+
126
+ #are equivalent to
127
+ sum(1..5) {|num| num} #=> 15
128
+ #and
129
+ (1..5).sum {|num| num} #=> 15
130
+
131
+ ### Input
132
+ Ranges and numeric arrays both work well.
133
+
134
+ sum(1..5) #=> 15
135
+ sum([1,2,3,4,5]) #=> 15
136
+
137
+ ## Note on Patches/Pull Requests
138
+
139
+ * Fork the project.
140
+ * Make your feature addition or bug fix.
141
+ * Add tests for it. This is important so I don't break it in a
142
+ future version unintentionally.
143
+ * Commit, do not mess with rakefile, version, or history.
144
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
145
+ * Send me a pull request. Bonus points for topic branches.
146
+
147
+ ## Copyright
148
+
149
+ Copyright (c) 2010 Allen Madsen. See LICENSE for details.
150
+
151
+ PS: Isn't it ridiculous how much documentation I wrote for one function?
62
152
  email: blatyo@gmail.com
63
153
  executables: []
64
154
 
@@ -76,9 +166,6 @@ files:
76
166
  - README.md
77
167
  - Rakefile
78
168
  - VERSION
79
- - features/addy.feature
80
- - features/step_definitions/addy_steps.rb
81
- - features/support/env.rb
82
169
  - lib/addy.rb
83
170
  - spec/addy_spec.rb
84
171
  - spec/spec.opts
@@ -1,9 +0,0 @@
1
- Feature: something something
2
- In order to something something
3
- A user something something
4
- something something something
5
-
6
- Scenario: something something
7
- Given inspiration
8
- When I create a sweet new gem
9
- Then everyone should see how awesome I am
File without changes
@@ -1,4 +0,0 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
- require 'addy'
3
-
4
- require 'spec/expectations'