every 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README +13 -0
- data/Rakefile +57 -0
- data/TODO +1 -0
- data/benchmarks.rb +32 -0
- data/every.gemspec +66 -0
- data/examples.rb +29 -0
- data/lib/every.rb +14 -0
- data/test/test_every.rb +23 -0
- data/test/test_helper.rb +9 -0
- metadata +64 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright © 2009 Martin Aumont (mynyml)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables.
|
2
|
+
|
3
|
+
compare:
|
4
|
+
|
5
|
+
enum = [1.4, 2.4 ,3.4]
|
6
|
+
enum.map {|i| i.floor } #=> [1, 2, 3]
|
7
|
+
enum.map(&:floor) #=> [1, 2, 3]
|
8
|
+
enum.every.floor #=> [1, 2, 3]
|
9
|
+
|
10
|
+
arguments? sure:
|
11
|
+
|
12
|
+
%w( axb dxf ).every.gsub(/x/,'y') #=> ['ayb', 'dyf']
|
13
|
+
%w( axb dxf ).every.gsub(/x/) { 'y' } #=> ['ayb', 'dyf']
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# --------------------------------------------------
|
2
|
+
# tasks mostly copied from thin's Rakefile
|
3
|
+
# http://github.com/macournoyer/thin/tree/master
|
4
|
+
# --------------------------------------------------
|
5
|
+
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'pathname'
|
8
|
+
require 'yaml'
|
9
|
+
|
10
|
+
RUBY_1_9 = RUBY_VERSION =~ /^1\.9/
|
11
|
+
WIN = (RUBY_PLATFORM =~ /mswin|cygwin/)
|
12
|
+
SUDO = (WIN ? "" : "sudo")
|
13
|
+
|
14
|
+
def gem
|
15
|
+
RUBY_1_9 ? 'gem19' : 'gem'
|
16
|
+
end
|
17
|
+
|
18
|
+
def all_except(paths)
|
19
|
+
Dir['**/*'] - paths.map {|path| path.strip.gsub(/^\//,'').gsub(/\/$/,'') }
|
20
|
+
end
|
21
|
+
|
22
|
+
spec = Gem::Specification.new do |s|
|
23
|
+
s.name = 'every'
|
24
|
+
s.version = '1.0'
|
25
|
+
s.summary = "Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables."
|
26
|
+
s.description = "Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables."
|
27
|
+
s.author = "Martin Aumont"
|
28
|
+
s.email = 'mynyml@gmail.com'
|
29
|
+
s.homepage = ''
|
30
|
+
s.has_rdoc = true
|
31
|
+
s.require_path = "lib"
|
32
|
+
s.files = Dir['**/*']
|
33
|
+
end
|
34
|
+
|
35
|
+
Rake::GemPackageTask.new(spec) do |p|
|
36
|
+
p.gem_spec = spec
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
desc "Remove package products"
|
41
|
+
task :clean => :clobber_package
|
42
|
+
|
43
|
+
desc "Update the gemspec for GitHub's gem server"
|
44
|
+
task :gemspec do
|
45
|
+
Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "Install gem"
|
49
|
+
task :install => [:clobber, :package] do
|
50
|
+
sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
|
51
|
+
end
|
52
|
+
|
53
|
+
desc "Uninstall gem"
|
54
|
+
task :uninstall => :clean do
|
55
|
+
sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
|
56
|
+
end
|
57
|
+
|
data/benchmarks.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/lib/env ruby
|
2
|
+
|
3
|
+
require 'pathname'
|
4
|
+
require 'benchmark'
|
5
|
+
root = Pathname(__FILE__).dirname
|
6
|
+
require root.join('lib/every.rb')
|
7
|
+
|
8
|
+
unless defined?(:a.to_proc)
|
9
|
+
class Symbol
|
10
|
+
def to_proc() proc { |obj| obj.__send__(self) } end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
puts
|
15
|
+
puts "One call"
|
16
|
+
n = 100_000
|
17
|
+
Benchmark.bmbm(15) do |bm|
|
18
|
+
bm.report('#map:') { (0..n).map {|i| i.floor } }
|
19
|
+
bm.report('Symbol#to_proc:') { (0..n).map(&:floor) }
|
20
|
+
bm.report('#every:') { (0..n).every.floor }
|
21
|
+
end
|
22
|
+
|
23
|
+
puts
|
24
|
+
puts "Two calls"
|
25
|
+
n = 100_000
|
26
|
+
Benchmark.bmbm(15) do |bm|
|
27
|
+
bm.report('#map:') { (0..n).map {|i| i.floor.next } }
|
28
|
+
bm.report('#map (x2):') { (0..n).map {|i| i.floor }.map {|i| i.next } }
|
29
|
+
bm.report('Symbol#to_proc:') { (0..n).map(&:floor).map(&:next) }
|
30
|
+
bm.report('#every:') { (0..n).every.floor.every.next }
|
31
|
+
bm.report('#every (chain):') { (0..n).every { floor.next } }
|
32
|
+
end
|
data/every.gemspec
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: every
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "1.0"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Aumont
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-19 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables.
|
17
|
+
email: mynyml@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- test
|
27
|
+
- test/test_every.rb
|
28
|
+
- test/test_helper.rb
|
29
|
+
- every.gemspec
|
30
|
+
- benchmarks.rb
|
31
|
+
- TODO
|
32
|
+
- lib
|
33
|
+
- lib/every.rb
|
34
|
+
- LICENSE
|
35
|
+
- examples.rb
|
36
|
+
- README
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: ""
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.3.3
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables.
|
65
|
+
test_files: []
|
66
|
+
|
data/examples.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'lib/every'
|
2
|
+
|
3
|
+
enum = [1.4, 2.4 ,3.4]
|
4
|
+
puts enum.every.floor.inspect
|
5
|
+
#=> [1, 2, 3]
|
6
|
+
|
7
|
+
enum = %w( axb dxf )
|
8
|
+
puts enum.every.gsub(/x/) { 'y' }.inspect
|
9
|
+
#=> ["ayb", "dyf"]
|
10
|
+
|
11
|
+
enum = %w( foo bar )
|
12
|
+
enum.every.upcase!
|
13
|
+
puts enum.inspect
|
14
|
+
#=> ["FOO", "BAR"]
|
15
|
+
|
16
|
+
enum = %w( foo bar ) << ''
|
17
|
+
puts enum.every.empty?.all?
|
18
|
+
#=> false
|
19
|
+
puts enum.every.empty?.any?
|
20
|
+
#=> true
|
21
|
+
|
22
|
+
enum = [{:name => 'Foo'}, {:name => 'Bar'}]
|
23
|
+
puts enum.every[:name].inspect
|
24
|
+
#=> ["Foo", "Bar"]
|
25
|
+
|
26
|
+
enum = [1,2,3]
|
27
|
+
puts (enum.every + 1).inspect
|
28
|
+
#puts enum.every.+(1).inspect
|
29
|
+
#=> [2, 3, 4]
|
data/lib/every.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
module Enumerable
|
2
|
+
class Proxy
|
3
|
+
instance_methods.each { |m| undef_method(m) unless m.match(/^__/) }
|
4
|
+
def initialize(enum, method=:map)
|
5
|
+
@enum, @method = enum, method
|
6
|
+
end
|
7
|
+
def method_missing(method, *args, &block)
|
8
|
+
@enum.__send__(@method) {|o| o.__send__(method, *args, &block) }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
module Enumerable
|
13
|
+
def every() Proxy.new(self) end
|
14
|
+
end
|
data/test/test_every.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'set'
|
3
|
+
root = Pathname(__FILE__).dirname.parent
|
4
|
+
require root.join('test/test_helper')
|
5
|
+
require root.join('lib/every')
|
6
|
+
|
7
|
+
class EveryTest < Test::Unit::TestCase
|
8
|
+
context "Every" do
|
9
|
+
test "is a basic object" do
|
10
|
+
whitelist = %w( __id__ __send__ method_missing )
|
11
|
+
Enumerable::Proxy.instance_methods.to_set.should be(whitelist.to_set)
|
12
|
+
end
|
13
|
+
test "passes message onto enumerable's items" do
|
14
|
+
[1.4, 2.4, 3.4].every.floor.should be([1,2,3])
|
15
|
+
end
|
16
|
+
test "allows arguments" do
|
17
|
+
%w( axb dxf ).every.gsub(/x/,'y').should be(%w( ayb dyf ))
|
18
|
+
end
|
19
|
+
test "allows blocks" do
|
20
|
+
%w( axb dxf ).every.gsub(/x/) { 'y' }.should be(%w( ayb dyf ))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: every
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "1.0"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Aumont
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-25 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables.
|
17
|
+
email: mynyml@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- test/test_every.rb
|
27
|
+
- test/test_helper.rb
|
28
|
+
- every.gemspec
|
29
|
+
- benchmarks.rb
|
30
|
+
- TODO
|
31
|
+
- lib/every.rb
|
32
|
+
- LICENSE
|
33
|
+
- examples.rb
|
34
|
+
- README
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: ""
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.5
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables.
|
63
|
+
test_files: []
|
64
|
+
|