# Strange inspect behaviour

**URL:** <https://www.ruby-forum.com/t/strange-inspect-behaviour/172215>\
**Category:** Ruby\
**Created:** [July 16, 2009, 9:32pm UTC](https://www.ruby-forum.com/t/strange-inspect-behaviour/172215 "2009-07-16T21:32:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chilaquil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/fbc32d/32.png) [@chilaquil](https://www.ruby-forum.com/u/chilaquil)\
**Post date:** [July 16, 2009, 9:32pm UTC](https://www.ruby-forum.com/t/strange-inspect-behaviour/172215/1 "2009-07-16T21:32:30Z")

</div>

The documentation says “if not overridden, #inspect method uses #to\_s to  
generate the string”, but it turns out that it’s behavior is more  
complicated: if it’s not overridden in some place, it uses #to\_s until  
the first instance variable assignment, after that it uses Object#to\_s

* * *

## ~$ cat \> test.rb class Test def assign @var = nil self end def to\_s “instance of Test” end end ~$ irb irb(main):001:0\> load ‘test.rb’ =\> true irb(main):002:0\> Test.new =\> instance of Test irb(main):003:0\> Test.new.assign =\> #\<Test:0x9371320 @var=nil\> irb(main):004:0\> RUBY\_VERSION =\> “1.9.1”

Is this a bug or it’s supposed to act like that?

---

<div class="post-metadata">

**Author:** ![chilaquil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/fbc32d/32.png) [@chilaquil](https://www.ruby-forum.com/u/chilaquil)\
**Post date:** [July 17, 2009, 10:19am UTC](https://www.ruby-forum.com/t/strange-inspect-behaviour/172215/2 "2009-07-17T10:19:56Z")

</div>

interesting, I didn’t know that…  
behaves the same on 1.8.7

Greetz!

---

<div class="post-metadata">

**Author:** ![chilaquil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/fbc32d/32.png) [@chilaquil](https://www.ruby-forum.com/u/chilaquil)\
**Post date:** [July 17, 2009, 10:22am UTC](https://www.ruby-forum.com/t/strange-inspect-behaviour/172215/3 "2009-07-17T10:22:58Z")

</div>

This code seems to make #inspect behave like it’s described in  
documentation  
class Object  
alias to\_s inspect  
def inspect() to\_s end  
end
