On 3/13/07, Trans <transfire / gmail.com> wrote:
> hought we wanted the result to be #<Hash:0xb75cfd18>?

rick@frodo:/public/rubyscripts$ ruby object_from_sc.rb
#<Class:#<Hash:0xb7df1774>>
{:a=>5}
oXb7df1774

And here's how:

rick@frodo:/public/rubyscripts$ cat object_from_sc.rb
# this method returns an integer value of the address of an object
with a standard
# object id.
# Note that internally Ruby FixedNums are stored as a value which is
the integer value
# shifted left one bit with a 1 bit ored into the LSB.
# For most objects the object_id is just the address with the LSB set.
 This method
# undoes this magic to reveal the actual address.
def hex_oid(obj)
  "oX#{(obj.object_id*2 & 0xffffffff).to_s(16)}"
end

c2 = class << {:a => 5}
  def mult(x)
    self[:a] * x
  end
  self
end

p c2

ObjectSpace.each_object(c2) {|o| p o; puts hex_oid(o)}

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/