mokayode mokayode - 5 months ago 14
Ruby Question

Is Socket bundled with Pry?

I'm learning socket in Ruby. I tried

require 'socket'
in a Pry session only to get
false
. After much trouble, I decided to use
irb
but things worked fine.

Later, I ran
TCPServer.new('localhost', 2000)
in a pry session without requiring
socket
and it worked!

I'm curious as to why it's so. Is it that
socket
is already part of pry? I checked Pry source code but could not figure it out.

Pry version 0.10.3 on Ruby 2.3.0

Answer

Yes socket is loaded in Pry when it starts. Whereas irb does not load it initially. You can always tells if a file has already been loaded because require will return false, as opposed to a LoadError.

2.3.1 :001 > require 'socket' # not yet loaded
# => true 
2.3.1 :002 > require 'socket' # already loaded
# => false 
2.3.1 :003 > require 'not_a_file' # error because it does not exist
# LoadError: cannot load such file -- not_a_file