Configuring Ruby REPLs
Both IRB and pry allow users to configure the REPL experience with configuration files, living in ~/.irbrc
and ~/.pryrc
files respectively. You can add all kinds of things to these files, but one thing I like to do is add a few methods.
Local methods and clipboard access
I include this snippet of code in both pryrc
and irbrc
files in my dotfiles.
class Object
def local_methods
(methods - Object.instance_methods).sort
end
def clipboard(text)
IO.write("|pbcopy", text)
end
end
The first method, local_methods
, makes it easy to see which methods exist on an object while ignoring the cruft (methods that exist on all objects).
With the clipboard
method in place, I can easily capture command output into my system clipboard. For example, I can capture the results from an ActiveRecord
query like so:
clipboard(Article.where(published: true))