# Getting Object#inspect back

**URL:** <https://www.ruby-forum.com/t/getting-object-inspect-back/71939>\
**Category:** Ruby\
**Created:** [October 12, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/getting-object-inspect-back/71939 "2006-10-12T15:46:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![OliverMarchand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e19adc/32.png) [@OliverMarchand](https://www.ruby-forum.com/u/OliverMarchand)\
**Post date:** [October 12, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/getting-object-inspect-back/71939/1 "2006-10-12T15:46:34Z")

</div>

Hello everybody,

Not that I despreately need it, but I was wondering how to e.g. get the  
Object#inspect method back for some classes which override it, say just  
as an example for a Hash?

Neither option I considered seems suitable:

alias\_method — too late, the original method is gone  
instance\_eval — didn’t work for me  
using .method and .call — I never seemed to link the objects self to  
the Object#inspect method

I am sure it is very easy…  
Oliver

---

<div class="post-metadata">

**Author:** ![OliverMarchand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e19adc/32.png) [@OliverMarchand](https://www.ruby-forum.com/u/OliverMarchand)\
**Post date:** [October 12, 2006, 4:06pm UTC](https://www.ruby-forum.com/t/getting-object-inspect-back/71939/2 "2006-10-12T16:06:45Z")

</div>

On 10/12/06, OliverMarchand [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> using .method and .call — I never seemed to link the objects self to  
> the Object#inspect method

This is tricky, because Object#inspect calls to\_s.  
You need to copy both.

class Foo \< Hash  
define\_method(:inspect, Object.instance\_method(:inspect))  
define\_method(:to\_s, Object.instance\_method(:to\_s))  
end

a = Foo.new  
p a # =\> #Foo:0xb7d26d2c

If you want to keep Hash to\_s (but why would anyone want that)  
you can do:

class Foo \< Hash  
define\_method(:inspect, Object.instance\_method(:to\_s))  
end

---

<div class="post-metadata">

**Author:** ![OliverMarchand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e19adc/32.png) [@OliverMarchand](https://www.ruby-forum.com/u/OliverMarchand)\
**Post date:** [October 12, 2006, 4:08pm UTC](https://www.ruby-forum.com/t/getting-object-inspect-back/71939/3 "2006-10-12T16:08:46Z")

</div>

Hi –

On Thu, 12 Oct 2006, OliverMarchand wrote:

> using .method and .call — I never seemed to link the objects self to  
> the Object#inspect method
> 
> I am sure it is very easy…

super might be adequate:

def inspect  
super  
end

unless there are multiple overridings along the way, in which case you  
might need:

Object.instance\_method(:inspect).bind(self).call

David

---

<div class="post-metadata">

**Author:** ![OliverMarchand](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e19adc/32.png) [@OliverMarchand](https://www.ruby-forum.com/u/OliverMarchand)\
**Post date:** [October 12, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/getting-object-inspect-back/71939/4 "2006-10-12T16:38:43Z")

</div>

On Thu, Oct 12, 2006 at 11:07:42PM +0900, removed\_email\_address@domain.invalid wrote:

> > Not that I despreately need it, but I was wondering how to e.g. get the  
> > Object#inspect method back for some classes which override it, say just  
> > as an example for a Hash?

[…]

> super might be adequate:  
> […]  
> unless there are multiple overridings along the way, in which case you  
> might need:
> 
> Object.instance\_method(:inspect).bind(self).call

It’s not enough in this case because there’s an explicit check in  
rb\_obj\_inspect that will make it use #to\_s if the object is not a  
“regular  
one” (i.e. not Array, Hash, File, etc. nor derived classes):

```
static VALUE
rb_obj_inspect(VALUE obj)
{
if (TYPE(obj) == T_OBJECT
    && ROBJECT(obj)->iv_tbl
    && ROBJECT(obj)->iv_tbl->num_entries > 0) {
/* .... */
}
return rb_funcall(obj, rb_intern("to_s"), 0, 0);
}

```

So even if you rebind Object#inspect, it won’t do what the OP wanted,  
but you  
can rebind Object#to\_s:

```
a = {}
a.inspect # => "{}"
ins = lambda{|o| Object.instance_method(:to_s).bind(o).call }
ins[a] # => "#<Hash:0xa7dc63cc>"
ins[[]] # => "#<Array:0xa7dc6200>"
ins[Object.new] # => "#<Object:0xa7dc60fc>"

```

e.g.

```
class Hash; define_method(:inspect, Object.instance_method(:to_s)) 

```

end  
{} # =\> #Hash:0xa7ddec38

However, Object#to\_s doesn’t display instance variables the way  
Object#inspect  
does:

```
a = {}
ins = lambda{|o| Object.instance_method(:to_s).bind(o).call }
a.instance_variable_set :@a, 1
ins[a] # => "#<Hash:0xa7d93490>"
o = Object.new
o.instance_variable_set :@a, 1
o.inspect # => "#<Object:0xa7d93364 @a=1>"
ins[o] # => "#<Object:0xa7d93364>"

```

so you’d have to implement another inspect, with recursivity checks and  
everything, if you want to imitate Object#inspect.
