2
votes

I have a hash:

hash = { test: 'Test' }

If I am in an irb session and I enter hash, it outputs the content of the hash:

{
  :test => 'Test'
}

What method is being invoked on the variable hash when I do that?

5

5 Answers

3
votes

The method is Hash#inspect.

hash = { test: 'Test' }
# => {:test=>"Test"}

hash.inspect
# => "{:test=>\"Test\"}"
2
votes
Object.inspect

The method is usually used for printing object structure.

2
votes

IRB calls #inspect method on your expressions and prints its result.

2
votes

When your hash contains a lot of data, it can be painful to read its content in one line.

I like to use y that print the hash in yaml.

h = {:a => 1, :b => 2}
y h
# --- 
#  :b: 2
#  :a: 1
# => nil
1
votes

IRB will call Hash#inspect.

hash.inspect