safe_clone 0.0.4 → 0.0.5
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 +4 -4
- data/README.md +67 -25
- data/irbt.rb +20 -0
- data/lib/safe_clone/version.rb +1 -1
- data/lib/safe_clone.rb +1 -1
- data/{Rakefile → rakefile.rb} +6 -1
- data/safe_clone.gemspec +1 -1
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 337deb97efeb6f36888f928f0ceb4a6aff7e1807
|
4
|
+
data.tar.gz: 85c5a520fcaaf4936282eb40227c46c38990b1ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfdfc38c09b4881333910b339fdface8878c357926c4e586a3f010bf0ac3b19619fd126254ac1aeec194ee297d91a688f8ff8b19452995ce29e3bc43baf09cc4
|
7
|
+
data.tar.gz: 94bc68f5b3f362cce6dc1050f5871ee18c5a33282691f31df09c177ed068e30818ed7a3911e22daa727d2ca21d89d2f1da044a3cd5acc5c28ec7dbd8b76e78d5
|
data/README.md
CHANGED
@@ -1,10 +1,45 @@
|
|
1
1
|
# SafeClone
|
2
2
|
|
3
|
-
This tiny gem implements a version of clone called safe\_clone.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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_clone 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
|
+
## Family Overview
|
23
|
+
|
24
|
+
This gem is a member of a family of four gems that all provide data copying
|
25
|
+
services in a safe, easy to use format. The following outlines the available
|
26
|
+
gems and how to chose from among them.
|
27
|
+
|
28
|
+
Depth / Action | Need to copy all. | Need to copy data only.
|
29
|
+
---------------|------------------------------|------------
|
30
|
+
Need a shallow copy | require 'safe\_clone' | require 'safe\_dup'
|
31
|
+
Need a full copy | require 'full\_clone' | require 'full\_dup'
|
32
|
+
|
33
|
+
<br>**Notes**
|
34
|
+
* Since none of these gems override the default clone and dup
|
35
|
+
methods, the default behaviors remain available. Further, if multiple,
|
36
|
+
differing requirements exists, more than one family member gem may be
|
37
|
+
employed in the same project without fear of conflict.
|
38
|
+
* If multiple family gems are employed, they will each need to be installed and
|
39
|
+
required into the application. See below for details.
|
40
|
+
* Meta-data attributes include the frozen status and singleton methods. However
|
41
|
+
the tainted status is always copied.
|
42
|
+
|
8
43
|
|
9
44
|
## Installation
|
10
45
|
|
@@ -20,6 +55,11 @@ Or install it yourself as:
|
|
20
55
|
|
21
56
|
$ gem install safe_clone
|
22
57
|
|
58
|
+
The safe_dup gem is at: ( https://rubygems.org/gems/safe_dup )
|
59
|
+
<br>The safe_clone gem is at: ( https://rubygems.org/gems/safe_clone )
|
60
|
+
<br>The full_dup gem is at: ( https://rubygems.org/gems/full_dup )
|
61
|
+
<br>The full_clone gem is at: ( https://rubygems.org/gems/full_clone )
|
62
|
+
|
23
63
|
## Usage
|
24
64
|
|
25
65
|
require 'safe_clone'
|
@@ -36,6 +76,15 @@ instead of
|
|
36
76
|
foo = my_object
|
37
77
|
end
|
38
78
|
|
79
|
+
It is actually pretty easy to determine where safe\_clone needs to be used. It's
|
80
|
+
those places where the clone method is generating unwanted exceptions.
|
81
|
+
|
82
|
+
## Demo
|
83
|
+
|
84
|
+
A test bed for experimenting with the safe_clone gem is available as a rake task:
|
85
|
+
|
86
|
+
$ rake console
|
87
|
+
|
39
88
|
## Performance
|
40
89
|
A reasonable question to raise is "How does safe clone compare with just
|
41
90
|
catching the exception and handling it?" The benchmark sets a a realistic
|
@@ -71,12 +120,7 @@ Benchmark.ips do |x|
|
|
71
120
|
end
|
72
121
|
```
|
73
122
|
|
74
|
-
|
75
|
-
|
76
|
-
ruby 1.9.3p484 (2013-11-22) [i386-mingw32]
|
77
|
-
|
78
|
-
Results:
|
79
|
-
|
123
|
+
#### Results: ruby 1.9.3p484 (2013-11-22) [i386-mingw32]
|
80
124
|
C:\Sites\safe_clone>ruby bench\bench.rb
|
81
125
|
Warming up --------------------------------------
|
82
126
|
Clone with standard clone method
|
@@ -93,12 +137,7 @@ Results:
|
|
93
137
|
Clone with the safe clone method: 534740.1 i/s
|
94
138
|
Clone with standard clone method: 12956.6 i/s - 41.27x slower
|
95
139
|
|
96
|
-
|
97
|
-
|
98
|
-
ruby 2.1.6p336 (2015-04-13 revision 50298) [i386-mingw32]
|
99
|
-
|
100
|
-
Results:
|
101
|
-
|
140
|
+
#### Results: ruby 2.1.6p336 (2015-04-13 revision 50298) [i386-mingw32]
|
102
141
|
C:\Sites\safe_clone>ruby bench\bench.rb
|
103
142
|
Warming up --------------------------------------
|
104
143
|
Clone with standard clone method
|
@@ -115,12 +154,7 @@ Results:
|
|
115
154
|
Clone with the safe clone method: 569236.4 i/s
|
116
155
|
Clone with standard clone method: 54491.3 i/s - 10.45x slower
|
117
156
|
|
118
|
-
|
119
|
-
|
120
|
-
ruby 2.2.3p173 (2015-08-18 revision 51636) [i386-cygwin]
|
121
|
-
|
122
|
-
Results:
|
123
|
-
|
157
|
+
#### Results: ruby 2.2.3p173 (2015-08-18 revision 51636) [i386-cygwin]
|
124
158
|
Peter Camilleri@NCC1701G /cygdrive/c/sites/safe_clone
|
125
159
|
$ ruby bench/bench.rb
|
126
160
|
Warming up --------------------------------------
|
@@ -143,8 +177,16 @@ Overall: Shorter code _and_ faster. Winner, winner, chicken dinner!
|
|
143
177
|
|
144
178
|
## Contributing
|
145
179
|
|
146
|
-
|
180
|
+
#### Plan A
|
181
|
+
|
182
|
+
1. Fork it ( https://github.com/PeterCamilleri/safe_clone/fork )
|
147
183
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
148
184
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
149
185
|
4. Push to the branch (`git push origin my-new-feature`)
|
150
|
-
5. Create new Pull Request
|
186
|
+
5. Create a new Pull Request
|
187
|
+
|
188
|
+
#### Plan B
|
189
|
+
|
190
|
+
Go to the GitHub repository and raise an issue calling attention to some
|
191
|
+
aspect that could use some TLC or a suggestion or an idea.
|
192
|
+
|
data/irbt.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# An IRB + safe_clone test bed
|
3
|
+
|
4
|
+
require 'irb'
|
5
|
+
$force_alias_read_line_module = true
|
6
|
+
require 'mini_readline'
|
7
|
+
|
8
|
+
puts "Starting an IRB console with safe_clone loaded."
|
9
|
+
|
10
|
+
if ARGV[0] == 'local'
|
11
|
+
require_relative 'lib/safe_clone'
|
12
|
+
puts "safe_clone loaded locally: #{SafeClone::VERSION}"
|
13
|
+
|
14
|
+
ARGV.shift
|
15
|
+
else
|
16
|
+
require 'safe_clone'
|
17
|
+
puts "safe_clone loaded from gem: #{SafeClone::VERSION}"
|
18
|
+
end
|
19
|
+
|
20
|
+
IRB.start
|
data/lib/safe_clone/version.rb
CHANGED
data/lib/safe_clone.rb
CHANGED
data/{Rakefile → rakefile.rb}
RENAMED
@@ -8,7 +8,12 @@ Rake::TestTask.new do |t|
|
|
8
8
|
t.verbose = false
|
9
9
|
end
|
10
10
|
|
11
|
-
desc "
|
11
|
+
desc "Fire up an IRB session with safe_clone preloaded."
|
12
|
+
task :console do
|
13
|
+
system "ruby irbt.rb local"
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "What version of safe_clone is this?"
|
12
17
|
task :vers do |t|
|
13
18
|
puts
|
14
19
|
puts "safe_clone version = #{SafeClone::VERSION}"
|
data/safe_clone.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
20
|
spec.add_development_dependency 'minitest_visible', ">= 0.1.0"
|
21
|
-
|
21
|
+
spec.add_development_dependency 'mini_readline', ">= 0.4.8"
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.3"
|
23
23
|
spec.add_development_dependency "rake"
|
24
24
|
spec.add_development_dependency 'minitest', "~> 5"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safe_clone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Camilleri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest_visible
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 0.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mini_readline
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.4.8
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.4.8
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -77,10 +91,11 @@ files:
|
|
77
91
|
- Gemfile
|
78
92
|
- LICENSE.txt
|
79
93
|
- README.md
|
80
|
-
- Rakefile
|
81
94
|
- bench/bench.rb
|
95
|
+
- irbt.rb
|
82
96
|
- lib/safe_clone.rb
|
83
97
|
- lib/safe_clone/version.rb
|
98
|
+
- rakefile.rb
|
84
99
|
- safe_clone.gemspec
|
85
100
|
- test/safe_clone_tests.rb
|
86
101
|
homepage: http://teuthida-technologies.com/
|