Set the default object in IRB

Given that we have the following class:

class User
  def name
    'John Doe'
  end

  def age
    30
  end
end

When playing with the User class instance in the ruby console, instead of calling the instance name all the time:

2.7.0 :001 > user = User.new
2.7.0 :002 > user.name # => 'John Doe'
2.7.0 :003 > user.age # => 30

we can set the default object and use the shorter version:

2.7.0 :001 > user = User.new
2.7.0 :002 > irb user
2.7.0 :003 > name # => 'John Doe'
2.7.0 :004 > age # => 30

It is really useful if you have to perform a lot of actions with the same class instance. Just remember to enter exit if you would like to exit from the default object.

#2
, 02-07-2021