Skip to content

Instantly share code, notes, and snippets.

@tagomoris
Created May 19, 2016 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tagomoris/ffa517adc675ee91340f14a49817cc79 to your computer and use it in GitHub Desktop.
Save tagomoris/ffa517adc675ee91340f14a49817cc79 to your computer and use it in GitHub Desktop.
MBA:~ tagomoris$ ruby objspace_test.rb
objspace_test.rb:14:in `block in <main>': can't convert Yay to Integer (Yay#to_int gives Yay) (TypeError)
from objspace_test.rb:12:in `each_object'
from objspace_test.rb:12:in `<main>'
MBA:~ tagomoris$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
require 'objspace'
class Yay < BasicObject
def method_missing(name, *args, &block)
self
end
end
y = Yay.new
map = {}
ObjectSpace.each_object {|obj|
klass = obj.class rescue Object
if c = map[klass]
map[klass] = c + 1
else
map[klass] = 1
end
}
p map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment