mynyml-every 0.6

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/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,48 @@
1
+ require 'rake/gempackagetask'
2
+ require 'pathname'
3
+ require 'yaml'
4
+
5
+ def gem
6
+ RUBY_1_9 ? 'gem19' : 'gem'
7
+ end
8
+
9
+ def all_except(paths)
10
+ Dir['**/*'] - paths.map {|path| path.strip.gsub(/^\//,'').gsub(/\/$/,'') }
11
+ end
12
+
13
+ spec = Gem::Specification.new do |s|
14
+ s.name = 'every'
15
+ s.version = '0.6'
16
+ s.summary = "Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables."
17
+ s.description = "Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables."
18
+ s.author = "Martin Aumont"
19
+ s.email = 'mynyml@gmail.com'
20
+ s.homepage = ''
21
+ s.has_rdoc = true
22
+ s.require_path = "lib"
23
+ s.files = Dir['**/*']
24
+ end
25
+
26
+ Rake::GemPackageTask.new(spec) do |p|
27
+ p.gem_spec = spec
28
+ end
29
+
30
+
31
+ desc "Remove package products"
32
+ task :clean => :clobber_package
33
+
34
+ desc "Update the gemspec for GitHub's gem server"
35
+ task :gemspec do
36
+ Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
37
+ end
38
+
39
+ desc "Install gem"
40
+ task :install => [:clobber, :package] do
41
+ sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
42
+ end
43
+
44
+ desc "Uninstall gem"
45
+ task :uninstall => :clean do
46
+ sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
47
+ end
48
+
data/TODO ADDED
@@ -0,0 +1 @@
1
+ o Use BasicObject if ruby 1.9
data/benchmarks.rb ADDED
@@ -0,0 +1,30 @@
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 iteration"
16
+ n = 100_000
17
+ Benchmark.bm(15) do |bm|
18
+ bm.report('Block:') { (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 iterations"
25
+ n = 100_000
26
+ Benchmark.bm(15) do |bm|
27
+ bm.report('Block:') { (0..n).map {|i| i.floor }.map {|i| i.next } }
28
+ bm.report('Symbol#to_proc:') { (0..n).map(&:floor).map(&:next) }
29
+ bm.report('every:') { (0..n).every.floor.every.next }
30
+ end
data/every.gemspec ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: every
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.6"
5
+ platform: ruby
6
+ authors:
7
+ - Martin Aumont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-18 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
+ - lib
26
+ - lib/every.rb
27
+ - examples
28
+ - examples/simple.rb
29
+ - LICENSE
30
+ - benchmarks.rb
31
+ - README
32
+ - every.gemspec
33
+ - Rakefile
34
+ - test
35
+ - test/test_helper.rb
36
+ - test/test_every.rb
37
+ - TODO
38
+ has_rdoc: true
39
+ homepage: ""
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.1
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables.
64
+ test_files: []
65
+
@@ -0,0 +1,10 @@
1
+ require 'pathname'
2
+ root = Pathname(__FILE__).dirname.parent
3
+ require root.join('lib/every')
4
+
5
+ enum = [1.4, 2.4 ,3.4]
6
+ puts enum.map {|i| i.floor }.inspect
7
+ puts enum.map(&:floor).inspect
8
+ puts enum.every.floor.inspect
9
+
10
+ puts %w( axb dxf ).every.gsub(/x/,'y').inspect
data/lib/every.rb ADDED
@@ -0,0 +1,15 @@
1
+ class Every
2
+ instance_methods.each { |m| undef_method(m) unless m.match(/^__/) }
3
+ def initialize(obj)
4
+ @obj = obj
5
+ end
6
+ def method_missing(method, *args, &block)
7
+ @obj.map {|o| o.__send__(method, *args, &block) }
8
+ end
9
+ end
10
+
11
+ module Enumerable
12
+ def every
13
+ Every.new(self)
14
+ end
15
+ end
@@ -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
+ Every.instance_methods.to_set.should be(whitelist.to_set)
12
+ end
13
+ test "calls method on 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
@@ -0,0 +1,9 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'context'
4
+ require 'matchy'
5
+ begin
6
+ require 'ruby-debug'
7
+ require 'quietbacktrace'
8
+ rescue LoadError, RuntimeError
9
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mynyml-every
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.6"
5
+ platform: ruby
6
+ authors:
7
+ - Martin Aumont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-17 21:00:00 -07: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
+ - lib
26
+ - lib/every.rb
27
+ - examples
28
+ - examples/simple.rb
29
+ - LICENSE
30
+ - benchmarks.rb
31
+ - README
32
+ - every.gemspec
33
+ - Rakefile
34
+ - test
35
+ - test/test_helper.rb
36
+ - test/test_every.rb
37
+ - TODO
38
+ has_rdoc: true
39
+ homepage: ""
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables.
64
+ test_files: []
65
+