Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Engel
committed
Jun 9, 2012
0 parents
commit 6475b27
Showing
13 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,6 @@ | |||
*.gem | |||
.bundle | |||
.rvmrc | |||
.DS_Store | |||
Gemfile.lock | |||
pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
= RubyMass CHANGELOG | |||
|
|||
== Version 0.1.0 (June 9, 2012) | |||
|
|||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
source "http://rubygems.org" | |||
|
|||
gemspec | |||
|
|||
group :gem_default do | |||
gem "ruby-mass", :path => "." | |||
end | |||
|
|||
group :gem_development do | |||
gem "pry" | |||
end | |||
|
|||
group :gem_test do | |||
gem "minitest" | |||
gem "mocha" | |||
gem "pry" | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
Copyright (c) 2012 Paul Engel | |||
|
|||
Permission is hereby granted, free of charge, to any person obtaining | |||
a copy of this software and associated documentation files (the | |||
"Software"), to deal in the Software without restriction, including | |||
without limitation the rights to use, copy, modify, merge, publish, | |||
distribute, sublicense, and/or sell copies of the Software, and to | |||
permit persons to whom the Software is furnished to do so, subject to | |||
the following conditions: | |||
|
|||
The above copyright notice and this permission notice shall be | |||
included in all copies or substantial portions of the Software. | |||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,50 @@ | |||
h1. RubyMass | |||
|
|||
Introspect the Ruby Heap by listing, counting, searching references to and detaching (in order to release) objects - optionally narrowing by namespace | |||
|
|||
h2. Introduction | |||
|
|||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |||
|
|||
h2. Installation | |||
|
|||
h3. Using Bundler | |||
|
|||
Add RubyMass in @Gemfile@ as a gem dependency: | |||
|
|||
<pre> | |||
gem "ruby-mass" | |||
</pre> | |||
|
|||
Run the following in your console to install with Bundler: | |||
|
|||
<pre> | |||
bundle install | |||
</pre> | |||
|
|||
h2. Usage | |||
|
|||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. | |||
|
|||
h2. Last remarks | |||
|
|||
Please check out "test/unit/test_objects.rb":https://github.com/archan937/ruby-mass/blob/master/test/unit/test_objects.rb. | |||
You can run the unit tests with @rake@ within the terminal. | |||
|
|||
Also, the RubyMass repo is provided with @script/console@ which you can use for testing purposes. | |||
|
|||
h2. Contact me | |||
|
|||
For support, remarks and requests please mail me at "paul.engel@holder.nl":mailto:paul.engel@holder.nl. | |||
|
|||
h2. License | |||
|
|||
Copyright (c) 2012 Paul Engel, released under the MIT license | |||
|
|||
"http://holder.nl":http://holder.nl – "http://codehero.es":http://codehero.es – "http://gettopup.com":http://gettopup.com – "http://github.com/archan937":http://github.com/archan937 – "http://twitter.com/archan937":http://twitter.com/archan937 – "paul.engel@holder.nl":mailto:paul.engel@holder.nl | |||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |||
|
|||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,9 @@ | |||
#!/usr/bin/env rake | |||
require "bundler/gem_tasks" | |||
require "rake/testtask" | |||
|
|||
task :default => :test | |||
|
|||
Rake::TestTask.new do |test| | |||
test.pattern = "test/**/test_*.rb" | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
0.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,147 @@ | |||
module Objects | |||
extend self | |||
|
|||
def [](object_id) | |||
ObjectSpace._id2ref object_id | |||
end | |||
|
|||
def references(object, mod = nil) | |||
instances_within(mod).inject({}) do |hash, instance| | |||
unless (refs = extract_references(instance, object)).empty? | |||
hash["#{instance.class.name}##{instance.object_id}"] = refs.sort{|a, b| a.to_s <=> b.to_s} | |||
end | |||
hash | |||
end | |||
end | |||
|
|||
def detach(object) | |||
detached = true | |||
instances_within(nil).each do |instance| | |||
extract_references(instance, object).each do |name| | |||
unless remove_references(instance, object, name) | |||
detached = false | |||
end | |||
end | |||
end | |||
GC.start | |||
detached | |||
end | |||
|
|||
def print(mod = nil) | |||
count(mod).tap do |stats| | |||
puts "\n\n" | |||
puts "=" * 50 | |||
puts " Objects within #{mod ? "#{mod.name} namespace" : "environment"}" | |||
puts "=" * 50 | |||
puts "\n" | |||
stats.keys.sort{|a, b| [stats[b], a] <=> [stats[a], b]}.each do |key| | |||
puts " #{key}: #{stats[key]}" | |||
end | |||
puts " (no objects instantiated)" if stats.empty? | |||
puts "\n" | |||
puts "=" * 50 | |||
puts "\n\n" | |||
end | |||
nil | |||
end | |||
|
|||
def count(mod = nil) | |||
group(mod).inject({}) do |hash, (key, value)| | |||
hash[key] = value.size | |||
hash | |||
end | |||
end | |||
|
|||
def group(mod = nil) | |||
instances_within(mod).inject({}) do |hash, object| | |||
(hash[object.class.name] ||= []) << object.object_id | |||
hash | |||
end | |||
end | |||
|
|||
private | |||
|
|||
def extract_references(instance, object) | |||
instance.instance_variables.select do |name| | |||
matches? instance.instance_variable_get(name), object | |||
end | |||
end | |||
|
|||
def matches?(variable, object) | |||
if variable.object_id == object.object_id | |||
true | |||
elsif variable.is_a?(Array) || variable.is_a?(Hash) | |||
variable.any?{|x| matches? x, object} | |||
else | |||
false | |||
end | |||
end | |||
|
|||
def remove_references(instance_or_variable, object, name = nil) | |||
detached = true | |||
variable = name ? instance_or_variable.instance_variable_get(name) : instance_or_variable | |||
|
|||
if name && variable.object_id == object.object_id | |||
instance_or_variable.send :remove_instance_variable, name | |||
elsif variable.is_a?(Array) | |||
detached = false | |||
variable.each do |value| | |||
if value.object_id == object.object_id | |||
detached = true | |||
variable.delete value | |||
elsif value.is_a?(Array) || value.is_a?(Hash) | |||
detached ||= remove_references(value, object) | |||
end | |||
end | |||
elsif variable.is_a?(Hash) | |||
detached = false | |||
variable.each do |key, value| | |||
if value.object_id == object.object_id | |||
detached = true | |||
variable.delete key | |||
elsif value.is_a?(Array) || value.is_a?(Hash) | |||
detached ||= remove_references(value, object) | |||
end | |||
end | |||
else | |||
detached = false | |||
end | |||
|
|||
detached | |||
end | |||
|
|||
def instances_within(mod) | |||
GC.start | |||
[].tap do |array| | |||
if mod.nil? | |||
ObjectSpace.each_object do |object| | |||
array << object | |||
end | |||
else | |||
classes_within(mod).each do |klass| | |||
ObjectSpace.each_object(klass) do |object| | |||
array << object | |||
end | |||
end | |||
end | |||
end | |||
end | |||
|
|||
def classes_within(mod, initial_array = nil) | |||
mod.constants.inject(initial_array || [(mod if mod.is_a? Class)].compact) do |array, c| | |||
const = mod.const_get c | |||
|
|||
if !array.include?(const) && (const.is_a?(Class) || const.is_a?(Module)) && const.name.match(/^#{mod.name}/) | |||
if const.is_a?(Class) | |||
array << const | |||
end | |||
if const.is_a?(Class) || const.is_a?(Module) | |||
classes_within(const, array) | |||
end | |||
end | |||
|
|||
array | |||
end | |||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
require "objects" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,16 @@ | |||
# -*- encoding: utf-8 -*- | |||
|
|||
Gem::Specification.new do |gem| | |||
gem.authors = ["Paul Engel"] | |||
gem.email = ["paul.engel@holder.nl"] | |||
gem.description = %q{Introspect the Ruby Heap by listing, counting, searching references to and detaching (releasing) objects - optionally narrowing by namespace} | |||
gem.summary = %q{Introspect the Ruby Heap by listing, counting, searching references to and detaching (releasing) objects - optionally narrowing by namespace} | |||
gem.homepage = "https://github.com/archan937/ruby-objects" | |||
|
|||
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } | |||
gem.files = `git ls-files`.split("\n") | |||
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") | |||
gem.name = "ruby-mass" | |||
gem.require_paths = ["lib"] | |||
gem.version = "0.1.0" | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,9 @@ | |||
#!/usr/bin/env ruby | |||
require "rubygems" | |||
require "bundler" | |||
|
|||
Bundler.require :gem_default, :gem_development | |||
|
|||
puts "Loading development environment" | |||
|
|||
Pry.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
require "rubygems" | |||
require "bundler" | |||
|
|||
Bundler.require :gem_default, :gem_test | |||
|
|||
require "minitest/unit" | |||
require "minitest/autorun" |
Empty file.