Created
December 21, 2010 13:52
-
-
Save deepak/749951 to your computer and use it in GitHub Desktop.
what does ObjectSpace.each_object(Module) work. what does match mean?
This file contains hidden or 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
| # in ruby 1.9.2-p0 | |
| require 'test/unit' | |
| class Foo < String | |
| end | |
| class ObSpace < Test::Unit::TestCase | |
| def setup | |
| @m = "hello" | |
| @n = Foo.new("hello") | |
| end | |
| def test_each_object | |
| assert_equal ["hello"].sort, ObjectSpace.each_object(@m.singleton_class).to_a.sort | |
| assert_equal [String, Foo].sort, ObjectSpace.each_object(@m.class.singleton_class).to_a.sort | |
| assert_equal ["hello"].sort, ObjectSpace.each_object(@n.singleton_class).to_a.sort | |
| assert_equal [Foo].sort, ObjectSpace.each_object(@n.class.singleton_class).to_a.sort | |
| end | |
| end | |
| __END__ | |
| # how does it work | |
| > If _module_ is specified, calls the block for only | |
| > those classes or modules that match (or are a subclass of) | |
| > _module | |
| what does match mean - what function is called to determine? | |
| why the bias for subclassing and inheritance | |
| why does giving the singleton class return the subclass of the eigenclass ie <x> and further subclassed of <x> | |
| $ ri ObjectSpace.each_object | |
| ----------------------------------------------- ObjectSpace::each_object | |
| ObjectSpace.each_object([module]) {|obj| ... } => fixnum | |
| ------------------------------------------------------------------------ | |
| Calls the block once for each living, nonimmediate object in this | |
| Ruby process. If _module_ is specified, calls the block for only | |
| those classes or modules that match (or are a subclass of) | |
| _module_. Returns the number of objects found. Immediate objects | |
| (+Fixnum+s, +Symbol+s +true+, +false+, and +nil+) are never | |
| returned. In the example below, +each_object+ returns both the | |
| numbers we defined and several constants defined in the +Math+ | |
| module. | |
| a = 102.7 | |
| b = 95 # Won't be returned | |
| c = 12345678987654321 | |
| count = ObjectSpace.each_object(Numeric) {|x| p x } | |
| puts "Total count: #{count}" | |
| _produces:_ | |
| 12345678987654321 | |
| 102.7 | |
| 2.71828182845905 | |
| 3.14159265358979 | |
| 2.22044604925031e-16 | |
| 1.7976931348623157e+308 | |
| 2.2250738585072e-308 | |
| Total count: 7 | |
| <manveru> imo there is no use-case for ObjectSpace outside of debugging MRI 6:08 PM | |
| <banisterfiend`> manveru: finding the attached class of the singleton 6:08 PM | |
| <banisterfiend`> is the only one ive used it for 6:08 PM | |
| <banisterfiend`> of a singleton* 6:08 PM | |
| <dkannan> manveru: in production i had once used ObjectSpace to reopen a log 6:08 PM | |
| <dkannan> manveru: in mongrel - per port logging 6:09 PM | |
| <dkannan> banisterfiend`: meaning traversing the object-graph the other-way around? 6:10 PM | |
| <manveru> dkannan: maybe in rubinius you'd have a chance of logging all string creations 6:10 PM | |
| <manveru> but then you'd create strings for the logging output... 6:11 PM | |
| <banisterfiend`> dkannan: j = "john"; j.singleton_class; ObjectSpace.each_object(j.singleton_class).to_a.first #=> "john" | |
| dkannan> banisterfiend`: j = "john"; j.singleton_class; ObjectSpace.each_object(j.singleton_class).to_a.first.object_id == j.object_id => true 6:18 PM | |
| <dkannan> banisterfiend`: j = "john"; class << j; def foo; puts "foo"; end; end; j.singleton_class; ObjectSpace.each_object(j.singleton_class).to_a.first.object_id == j.object_id 6:18 PM | |
| <banisterfiend`> dkannan: yea 6:18 PM | |
| <dkannan> banisterfiend`: did not understand j.singleton_class.class => Class 6:20 PM | |
| <banisterfiend`> dkannan: what did you expect the class to be 6:21 PM | |
| <dkannan> banisterfiend`: why is it diff from: ObjectSpace.each_object(Class).to_a 6:21 PM | |
| <banisterfiend`> hmmm 6:21 PM | |
| <banisterfiend`> dkannan: i dont get your q 6:22 PM | |
| <dkannan> banisterfiend`: when we say ObjectSpace.each_object(j.singleton_class) 6:22 PM | |
| <dkannan> banisterfiend`: what objects are we iteratuing tru? 6:23 PM | |
| <banisterfiend`> how could it be the same 6:23 PM | |
| <banisterfiend`> dkannan: instances of the argument to each_object 6:23 PM | |
| <dkannan> banisterfiend`: i cannot say: class Foo < j.singleton_class; def foo; end; end; 6:25 PM | |
| <banisterfiend`> try it hehe 6:25 PM | |
| <banisterfiend`> yeah u cant 6:25 PM | |
| <raggi> heh 6:25 PM | |
| <banisterfiend`> interesting error msg 6:26 PM | |
| <banisterfiend`> superclass mismatch 6:26 PM | |
| <raggi> heh 6:26 PM | |
| <raggi> superclazzzzz is not classszzzz 6:26 PM | |
| <banisterfiend`> dkannan: try j.singleton_class.new 6:26 PM | |
| <banisterfiend`> raggi: r u drunk 6:27 PM | |
| <banisterfiend`> heehe 6:27 PM | |
| <raggi> banisterfiend`: tired, i haven't slept 6:27 PM | |
| <dkannan> banisterfiend`: did not understand what happens when u pass a module to ObjectSpace#each_instance http://pastie.org/private/m7qprrpndf5z5d2oxtc5a 7:03 PM | |
| <dkannan> banisterfiend`: what does match mean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment