Forum: Ruby-core ObjectSpace.reference_form(obj) #=> references_array

Posted by SASADA Koichi (Guest)
on 2011-09-29 05:31
(Received via mailing list)
Hi,

I wrote a patch of ObjectSpace.reference_form(obj).  This method returns
references that this object has.

example:
require 'objspace'

h = {'a' => [1, 2, 3], 'b' => Object.new}
p ObjectSpace.reference_from(h)

#=>

{76577650=>Hash, 76559320=>"a", 76559360=>[1, 2, 3], 76559310=>"b",
76559340=>#<Object:0x92067d8>}

Hash keys are object id and values are objects themselves.


Patch:
  http://www.atdot.net/sp/view/apl9sl/readonly


How about it?  Good name is also welcome.


# Flight from Porland to New Orleans hack.
Posted by SASADA Koichi (Guest)
on 2011-09-29 05:34
(Received via mailing list)
Sorry, I missed the subject.  Returns not an array but a hash object.
Posted by Nobuyoshi Nakada (nobu)
on 2011-09-29 14:05
(Received via mailing list)
Hi,

(11/09/29 12:30), SASADA Koichi wrote:
> {76577650=>Hash, 76559320=>"a", 76559360=>[1, 2, 3], 76559310=>"b",
> 76559340=>#<Object:0x92067d8>}
>
> Hash keys are object id and values are objects themselves.

Why object id hash, but not a plain array?
Posted by Hemant Kumar (gnufied)
on 2011-09-30 12:38
(Received via mailing list)
Hi,

2011/9/29 Nobuyoshi Nakada <nobu@ruby-lang.org>:
> Hi,
>
> (11/09/29 12:30), SASADA Koichi wrote:
>> {76577650=>Hash, 76559320=>"a", 76559360=>[1, 2, 3], 76559310=>"b",
>> 76559340=>#<Object:0x92067d8>}
>>
>> Hash keys are object id and values are objects themselves.
>
> Why object id hash, but not a plain array?


I think reference_from is a bit ambiguous as it stands. How about 
"contains" ?

I am not sure, what values are in the output though. Are they Type or
output of inspect?


--
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://gnufied.org
http://twitter.com/gnufied
Posted by SASADA Koichi (Guest)
on 2011-09-30 16:08
(Received via mailing list)
(2011/09/30 5:37), hemant wrote:
> I think reference_from is a bit ambiguous as it stands. How about "contains" ?

ObjectSpace.contains(obj)  ?

> I am not sure, what values are in the output though. Are they Type or
> output of inspect?


As I wrote in first post:

> h = {'a' => [1, 2, 3], 'b' => Object.new}
> p ObjectSpace.reference_from(h)
> #=>
> {76577650=>Hash, 76559320=>"a", 76559360=>[1, 2, 3], 76559310=>"b",
> 76559340=>#<Object:0x92067d8>}
>
> Hash keys are object id and values are objects themselves.

Output is inspected string by "p".
Posted by Tim Felgentreff (timfelgentreff)
on 2011-10-01 17:17
(Received via mailing list)
On 09/30/2011 07:08 AM, SASADA Koichi wrote:
> (2011/09/30 5:37), hemant wrote:
>> I think reference_from is a bit ambiguous as it stands. How about "contains" ?
>
> ObjectSpace.contains(obj)  ?

#reachable_set or #transitive_closure seem to be the names that are used
in graph theory papers. Maybe we should try stick to what's already 
there.

>> {76577650=>Hash, 76559320=>"a", 76559360=>[1, 2, 3], 76559310=>"b",
>> 76559340=>#<Object:0x92067d8>}
>>
>> Hash keys are object id and values are objects themselves.
>
> Output is inspected string by "p".
>

What are the opinions on adding an official mirror API to Ruby to
separate reflection from non-metalevel stuff and doing things like that
on mirrors in the future? Adding more functions to ObjectSpace et.al.
doesn't seem like good OO to me in the long run. How about having

ObjectMirror.reflect_on(h).transitive_closure?

or similar? There's a good paper by Gilad Bracha and David Ungar,

"Mirrors: Design Principles for Meta-level Facilities of Object-Oriented
Programming Languages"

that we could draw ideas from.
Posted by SASADA Koichi (Guest)
on 2012-09-21 03:15
(Received via mailing list)
Revisit.

I made a patch:
https://github.com/ko1/ruby/tree/all_refs

  ObjectSpace.all_references_from(obj) #=> [...]

I don't any strong idea about naming.

  - ObjectSpace.contains(obj)
  - ObjectSpace.reachable_set(obj)
  - ObjectSpace.transitive_closure(obj)

"ObjectSpace.reachable_objects_from(obj)" is more descriptive?
Posted by Eric Hodel (Guest)
on 2012-09-25 00:39
(Received via mailing list)
On Sep 20, 2012, at 6:14 PM, SASADA Koichi <ko1@atdot.net> wrote:
>  - ObjectSpace.reachable_set(obj)
>  - ObjectSpace.transitive_closure(obj)
>
> "ObjectSpace.reachable_objects_from(obj)" is more descriptive?

I think this name is the most descriptive.
Posted by SASADA Koichi (Guest)
on 2012-09-25 06:53
(Received via mailing list)
(2012/09/25 7:38), Eric Hodel wrote:
>> > "ObjectSpace.reachable_objects_from(obj)" is more descriptive?
> I think this name is the most descriptive.
>

Thanks.

Matz, nari-san, can I apply this patch?
Posted by Narihiro Nakamura (Guest)
on 2012-09-25 08:19
(Received via mailing list)
I'm sorry for late reply.

2012/9/25 SASADA Koichi <ko1@atdot.net>:
> (2012/09/25 7:38), Eric Hodel wrote:
>>> > "ObjectSpace.reachable_objects_from(obj)" is more descriptive?
>> I think this name is the most descriptive.
>>

Do you have any use cases?

Nakada-san says:
> Why object id hash, but not a plain array?

How about it?
Posted by SASADA Koichi (Guest)
on 2012-09-25 08:26
(Received via mailing list)
(2012/09/25 15:18), Narihiro Nakamura wrote:
> Do you have any use cases?

Analysis.  For example, find out memory leak.

> Nakada-san says:
>> > Why object id hash, but not a plain array?
> How about it?

The latest patch returns a plain array or nil.
Posted by Narihiro Nakamura (Guest)
on 2012-09-25 09:20
(Received via mailing list)
2012/9/25 SASADA Koichi <ko1@atdot.net>:
>
Ahh, I see.

Please commit it if matz accepts it.

Thanks.
Posted by SASADA Koichi (Guest)
on 2012-10-05 08:21
(Received via mailing list)
(2012/09/25 16:19), Narihiro Nakamura wrote:
> Please commit it if matz accepts it.

I got matz approval.

I'll commit `ObjectSpace.reachable_objects_from(obj)'.

Thanks,
Koichi
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.