Paul
Paul

Reputation: 26640

Print memory address for Ruby array

irb> class A; end
=> nil
irb> a=A.new
=> "#<A:0x3094638>"
irb> a.inspect
=> "#<A:0x3094638>"
irb> b=[]
=> []
irb> b.inspect
=> "[]"

How to get memory address of an array object?

Upvotes: 0

Views: 1389

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118261

Use the method Object#object_id.

Returns an integer identifier for obj. The same number will be returned on all calls to id for a given object, and no two active objects will share an id. #object_id is a different concept from the :name notation, which returns the symbol id of name.

Example :-

Arup-iMac:arup_ruby $ irb
2.1.2 :001 > s = "I am a string"
 => "I am a string" 
2.1.2 :002 > obj_id = s.object_id
 => 2156122060 
2.1.2 :003 > ObjectSpace._id2ref obj_id
 => "I am a string" 
2.1.2 :004 > 

Upvotes: 5

Related Questions