Skip to content

Instantly share code, notes, and snippets.

@assadk88
Forked from mkhl/drop-to-irb.rb
Created October 5, 2019 16:50
Show Gist options
  • Save assadk88/196f57bfbe0586d98293619467642af1 to your computer and use it in GitHub Desktop.
Save assadk88/196f57bfbe0586d98293619467642af1 to your computer and use it in GitHub Desktop.
Collection of IRB tricks
require 'irb'
module IRB
def self.start_session(binding)
IRB.setup(nil)
workspace = WorkSpace.new(binding)
if @CONF[:SCRIPT]
irb = Irb.new(workspace, @CONF[:SCRIPT])
else
irb = Irb.new(workspace)
end
@CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
@CONF[:MAIN_CONTEXT] = irb.context
trap("SIGINT") do
irb.signal_handle
end
catch(:IRB_EXIT) do
irb.eval_input
end
end
end
# we want to manipulate this in IRB
two = 1 + 1
IRB.start_session(Kernel.binding)
### Example Session:
# $ ruby debug.rb
# >> two
# => 2
# >> three
# NameError: undefined local variable or method `three' for Kernel:Module
# from (irb):1
# from debug.rb:31
# Tab-completion using readline
require 'irb/completion'
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
# persistent IRB command history
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
# Simpler prompt
IRB.conf[:PROMPT_MODE] = :SIMPLE
# Dr. Nic's .irbrc:
require 'irb/completion' # Tab-completion
require 'map_by_method' # Mapping by method (a.map_to_s = a.map &:to_s)
require 'what_methods' # MethodFinder
require 'pp' # Pretty-printing
IRB.conf[:AUTO_INDENT]=true # Auto indentation
# Execute file every time it changes
def loop_execute(file)
old_mtime = nil
loop do
# print("\e[sWaiting...")
sleep(0.2) while (mtime = File.stat(file).mtime) == old_mtime
# print("\e[u\e[K")
begin
r = eval(File.read(file))
puts("=> #{r.inspect}")
rescue IRB::Abort
puts("Abort")
return
rescue Exception => e
puts("#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}")
end
old_mtime = mtime
end
end
# Running shell commands from inside IRB
# Based on <http://woss.name/2006/07/12/using-the-shell-within-irb/> by:
# Graeme Mathieson
require 'shell'
# Override the command processor widget for inserting system commands so
# that it behaves more like path-processing: earlier commands take precedence.
require 'shell/command-processor'
module FixAddDelegateCommandToShell
def self.extended(obj)
class << obj
alias_method :add_delegate_command_to_shell_override, :add_delegate_command_to_shell unless method_defined?(:add_delegate_command_to_shell_override)
alias_method :add_delegate_command_to_shell, :add_delegate_command_to_shell_no_override
end
end
def add_delegate_command_to_shell_no_override(id)
id = id.intern if id.kind_of?(String)
name = id.id2name
if Shell.method_defined?(id) or Shell::Filter.method_defined?(id)
Shell.notify "warn: Not overriding existing definition of Shell##{name}."
else
add_delegate_command_to_shell_override(id)
end
end
end
Shell::CommandProcessor.extend(FixAddDelegateCommandToShell)
# Allow Shell system commands to take :symbols too, to save a little typing.
require 'shell/system-command'
class Shell
class SystemCommand
alias_method :initialize_orig, :initialize
def initialize(sh, command, *opts)
opts.collect! {|opt| opt.to_s }
initialize_orig sh, command, *opts
end
end
end
# Provide me with a shell inside IRB to save quitting and restarting, or
# finding that other terminal window.
def shell
unless $shell
verbose = Shell.verbose
Shell.verbose = false
Shell.install_system_commands '' # no prefix
$shell = Shell.new
Shell.verbose = verbose
end
$shell
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment