safe_dup 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 551e1417893bcf4fea7633327e7c9af155d8ba9e
4
+ data.tar.gz: 093304018762e3b0bf48c4e2619fa18efe62c14b
5
+ SHA512:
6
+ metadata.gz: c0d22238e157ff8d7a6077ccb1bfc5d5bad41a8cbc10d3227b4bff6a26b2c5609731ccc385f59915286e8da081fbd494d3a97584ab869e774ce95c22204c0376
7
+ data.tar.gz: 242874925de15eaf0a13f72b0677436ed96022e6ab46ed264362355aebd05ddff84cc297b5bbc19f219009e3bce52c1139d6eeb866d21882f9ca257a4321a87e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in safe_dup.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Peter Camilleri
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,159 @@
1
+ # SafeDup
2
+
3
+ This tiny gem implements a version of clone called safe\_clone. In Ruby, if an
4
+ attempt is made to clone an immutable data item like a number, an error occurs.
5
+ The justification for this uncharacteristic strictness is not at all clear, but
6
+ it does mean that the clone operation must be applied with great care.
7
+
8
+ Unlike the standard clone method, the safe\_clone method does not throw an
9
+ exception when sent to un-clonable value objects like 42 or true. These values
10
+ simply return themselves. This is correct because those types of objects do
11
+ not _need_ to be cloned. Instead of having a fit, the code just works!
12
+
13
+ On a note about performance, this gem does not just rescue the exceptions
14
+ normally generated by clone, it prevents them from occurring and wasting time
15
+ in the first place.
16
+
17
+ Finally, this gem does not monkey patch the behavior of the clone method.
18
+ Modifying such a crucial method was considered too risky. Instead, the
19
+ safe_dup method is introduced. This is done to reduce the possibility of
20
+ breaking existing code that often occurs when monkey patching goes too far.
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ gem 'safe_dup'
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install safe_dup
35
+
36
+ ## Usage
37
+
38
+ require 'safe_dup'
39
+
40
+ then, in those places where regular clone was problematic, use:
41
+
42
+ foo = my_object.safe_dup
43
+
44
+ instead of
45
+
46
+ begin
47
+ foo = my_object.clone
48
+ rescue TypeError
49
+ foo = my_object
50
+ end
51
+
52
+ It is actually pretty easy to determine where safe\_clone needs to be used. It's
53
+ those places where the clone method is generating unwanted exceptions.
54
+
55
+ ## Performance
56
+ A reasonable question to raise is "How does safe clone compare with just
57
+ catching the exception and handling it?" The benchmark sets a a realistic
58
+ scenario where an array (whose contents may be varied) is having its
59
+ _contents_ cloned. The benchmarking code follows:
60
+
61
+ ```ruby
62
+ require "benchmark/ips"
63
+ require 'safe_dup'
64
+
65
+ class Array
66
+ def use_dup
67
+ self.map do |element|
68
+ begin
69
+ element.dup
70
+ rescue TypeError
71
+ element
72
+ end
73
+ end
74
+ end
75
+
76
+ def use_safe_dup
77
+ self.map {|element| element.safe_dup }
78
+ end
79
+ end
80
+
81
+ X = ["Test", :test, 43, true, nil, false]
82
+
83
+ Benchmark.ips do |x|
84
+ x.report("Clone with standard clone method") { X.use_dup }
85
+ x.report("Clone with the safe clone method") { X.use_safe_dup }
86
+ x.compare!
87
+ end
88
+ ```
89
+
90
+ #### Results: ruby 1.9.3p484 (2013-11-22) [i386-mingw32]
91
+ C:\Sites\safe_dup>ruby bench\bench.rb
92
+ Warming up --------------------------------------
93
+ Clone with standard clone method
94
+ 1.247k i/100ms
95
+ Clone with the safe clone method
96
+ 35.027k i/100ms
97
+ Calculating -------------------------------------
98
+ Clone with standard clone method
99
+ 12.957k (± 5.8%) i/s - 64.844k
100
+ Clone with the safe clone method
101
+ 534.740k (± 8.9%) i/s - 2.662M
102
+
103
+ Comparison:
104
+ Clone with the safe clone method: 534740.1 i/s
105
+ Clone with standard clone method: 12956.6 i/s - 41.27x slower
106
+
107
+ #### Results: ruby 2.1.6p336 (2015-04-13 revision 50298) [i386-mingw32]
108
+ C:\Sites\safe_dup>ruby bench\bench.rb
109
+ Warming up --------------------------------------
110
+ Clone with standard clone method
111
+ 4.945k i/100ms
112
+ Clone with the safe clone method
113
+ 38.109k i/100ms
114
+ Calculating -------------------------------------
115
+ Clone with standard clone method
116
+ 54.491k (± 7.3%) i/s - 271.975k
117
+ Clone with the safe clone method
118
+ 569.236k (±10.2%) i/s - 2.820M
119
+
120
+ Comparison:
121
+ Clone with the safe clone method: 569236.4 i/s
122
+ Clone with standard clone method: 54491.3 i/s - 10.45x slower
123
+
124
+ #### Results: ruby 2.2.3p173 (2015-08-18 revision 51636) [i386-cygwin]
125
+ Peter Camilleri@NCC1701G /cygdrive/c/sites/safe_dup
126
+ $ ruby bench/bench.rb
127
+ Warming up --------------------------------------
128
+ Clone with standard clone method
129
+ 3.698k i/100ms
130
+ Clone with the safe clone method
131
+ 28.999k i/100ms
132
+ Calculating -------------------------------------
133
+ Clone with standard clone method
134
+ 40.076k (± 5.1%) i/s - 203.390k
135
+ Clone with the safe clone method
136
+ 481.524k (±10.0%) i/s - 2.407M
137
+
138
+ Comparison:
139
+ Clone with the safe clone method: 481524.1 i/s
140
+ Clone with standard clone method: 40075.6 i/s - 12.02x slower
141
+
142
+
143
+ Overall: Shorter code _and_ faster. Winner, winner, chicken dinner!
144
+
145
+ ## Contributing
146
+
147
+ #### Plan A
148
+
149
+ 1. Fork it ( https://github.com/PeterCamilleri/safe_dup/fork )
150
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
151
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
152
+ 4. Push to the branch (`git push origin my-new-feature`)
153
+ 5. Create a new Pull Request
154
+
155
+ #### Plan B
156
+
157
+ Go to the GitHub repository and raise an issue calling attention to some
158
+ aspect that could use some TLC or a suggestion or an idea.
159
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rake/testtask'
2
+ require "bundler/gem_tasks"
3
+
4
+ #Run the unit test suite.
5
+ Rake::TestTask.new do |t|
6
+ #List out all the test files.
7
+ t.test_files = ["test/safe_dup_tests.rb"]
8
+ t.verbose = false
9
+ end
10
+
11
+ desc "What version of full_clone is this?"
12
+ task :vers do |t|
13
+ puts
14
+ puts "safe_dup version = #{SafeDup::VERSION}"
15
+ end
@@ -0,0 +1,3 @@
1
+ module SafeDup
2
+ VERSION = "0.0.1"
3
+ end
data/lib/safe_dup.rb ADDED
@@ -0,0 +1,21 @@
1
+
2
+ require_relative "safe_dup/version"
3
+
4
+ class Object
5
+ #By default, reference types use the clone method.
6
+ def safe_dup; self.dup; end
7
+ end
8
+
9
+ #For value types, just return self!
10
+ module SafeDup
11
+ def safe_dup; self; end
12
+ end
13
+
14
+ #Update the Ruby value types.
15
+ class Numeric; include SafeDup; end
16
+ class NilClass; include SafeDup; end
17
+ class TrueClass; include SafeDup; end
18
+ class FalseClass; include SafeDup; end
19
+ class Symbol; include SafeDup; end
20
+ class Regexp; include SafeDup; end
21
+
data/safe_dup.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'safe_dup/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "safe_dup"
8
+ spec.version = SafeDup::VERSION
9
+ spec.authors = ["Peter Camilleri"]
10
+ spec.email = ["peter.c.camilleri@gmail.com"]
11
+
12
+ spec.summary = "A safer version of the dup method that avoids unnecessary exceptions."
13
+ spec.description = "A safer version of the clone method."
14
+ spec.homepage = "http://teuthida-technologies.com/"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency 'minitest_visible', ">= 0.1.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency 'minitest', "~> 5"
26
+ end
@@ -0,0 +1,54 @@
1
+ # coding: utf-8
2
+
3
+ require_relative '../lib/safe_dup'
4
+ gem 'minitest'
5
+ require 'minitest/autorun'
6
+ require 'minitest_visible'
7
+
8
+ #Test the monkey patches applied to the Object class.
9
+ class SafeDupTester < Minitest::Test
10
+
11
+ #Track mini-test progress.
12
+ include MinitestVisible
13
+
14
+ def test_for_safe_value_cloning
15
+ assert_equal((6).safe_dup, 6)
16
+ assert_equal((6).safe_dup.object_id, (6).object_id)
17
+
18
+ assert_equal((:foo).safe_dup, :foo)
19
+ assert_equal((:foo).safe_dup.object_id, (:foo).object_id)
20
+
21
+ assert_equal((true).safe_dup, true)
22
+ assert_equal((true).safe_dup.object_id, (true).object_id)
23
+
24
+ assert_equal((false).safe_dup, false)
25
+ assert_equal((false).safe_dup.object_id, (false).object_id)
26
+
27
+ assert_equal((nil).safe_dup, nil)
28
+ assert_equal((nil).safe_dup.object_id, (nil).object_id)
29
+
30
+ rex = /ABC/
31
+ assert_equal(rex.safe_dup, rex)
32
+ assert_equal(rex.safe_dup.object_id, rex.object_id)
33
+ end
34
+
35
+ def test_for_reference_cloning
36
+ str = "test"
37
+ assert_equal(str.safe_dup, str)
38
+ refute_equal(str.safe_dup.object_id, str.object_id)
39
+
40
+ arra = [1,2,3]
41
+ arrb = arra.safe_dup
42
+ assert_equal(arrb, arra)
43
+ refute_equal(arrb.object_id, arra.object_id)
44
+
45
+ arrb << 4
46
+ assert_equal([1,2,3], arra)
47
+ assert_equal([1,2,3,4], arrb)
48
+
49
+ lam = lambda { puts 'hello world'}
50
+ refute_equal(lam.safe_dup.object_id, lam.object_id)
51
+
52
+ end
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safe_dup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Camilleri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest_visible
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5'
69
+ description: A safer version of the clone method.
70
+ email:
71
+ - peter.c.camilleri@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/safe_dup.rb
82
+ - lib/safe_dup/version.rb
83
+ - safe_dup.gemspec
84
+ - test/safe_dup_tests.rb
85
+ homepage: http://teuthida-technologies.com/
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: A safer version of the dup method that avoids unnecessary exceptions.
109
+ test_files:
110
+ - test/safe_dup_tests.rb
111
+ has_rdoc: