From: Mauricio Fernandez Date: 2006-10-12T23:37:08+09:00 Subject: Re: getting Object#inspect back On Thu, Oct 12, 2006 at 11:07:42PM +0900, dblack@wobblini.net 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] # => "#" ins[[]] # => "#" ins[Object.new] # => "#" e.g. class Hash; define_method(:inspect, Object.instance_method(:to_s)) end {} # => # 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] # => "#" o = Object.new o.instance_variable_set :@a, 1 o.inspect # => "#" ins[o] # => "#" so you'd have to implement another inspect, with recursivity checks and everything, if you want to imitate Object#inspect. -- Mauricio Fernandez - http://eigenclass.org - singular Ruby