Skip to content

Instantly share code, notes, and snippets.

@dolzenko
Created January 8, 2014 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolzenko/8317445 to your computer and use it in GitHub Desktop.
Save dolzenko/8317445 to your computer and use it in GitHub Desktop.
irb_callbacks
= irb_callbacks
* http://rubysideshow.rubyforge.org/irb_callbacks
== DESCRIPTION:
This gem adds callbacks to irb, intended for you to
override at your discretion.
== FEATURES:
irb's control flow looks like this:
loop:
* prompt
* eval
* output
This gem adds three callbacks to each phase.
module IRB:
* self.before_prompt
* self.around_prompt (call yield)
* self.after_prompt
* self.before_eval
* self.around_eval (call yield)
* self.after_eval
* self.before_output
* self.around_output (call yield)
* self.after_output
== SYNOPSIS:
# Here's my ~/.irbrc file (which is run at irb startup)
require 'rubygems'
require 'irb_callbacks'
require 'benchmark'
# This little snippet will time each command run via the console.
module IRB
def self.around_eval(&block)
@timing = Benchmark.realtime do
block.call
end
end
def self.after_output
puts "=> #{'%.3f' % @timing} seconds"
end
end
# And a sample irb session:
$ irb
irb(main):001:0> 1_000_000.times { |x| x + 1 }
=> 1000000
=> 0.330 seconds
== CAVEATS:
The three around_* callbacks all require you to call the
block that's passed in. If you don't do it, undefined
behavior may occur.
== INSTALL:
* sudo gem install irb_callbacks
== LICENSE:
(The MIT License)
Copyright (c) 2008 Mike Judge
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'irb'
#
# irb's control flow looks like this:
#
# loop:
#
# * prompt
# * eval
# * output
#
# This gem adds three callbacks to each
# phase, so you can override and provide
# your own functionality.
#
# module IRB:
#
# * self.before_prompt
# * self.around_prompt (call yield)
# * self.after_prompt
#
# * self.before_eval
# * self.around_eval (call yield)
# * self.after_eval
#
# * self.before_output
# * self.around_output (call yield)
# * self.after_output
#
# Some ideas to get you started:
#
# * automatic benchmarking
#
# (see the readme)
#
# * examine objectspace for leaks
#
# * tail the rails logs from irb
#
# You can automate irb to load up your changes
# each time you fire it up by adding the following
# to .irbrc in your home directory:
#
# require 'rubygems'
# require 'irb_callbacks'
#
# ... your code ...
#
# If you've got any requests or complaints,
# please e-mail mikelovesrobots@gmail.com.
#
class IrbCallbacks
VERSION = '0.1.0'
end
class RubyLex
alias original_lex lex
# Add before_prompt, after_prompt, and around_prompt callbacks
# without damaging original behavior.
def lex(*args, &block)
IRB::before_prompt
value = nil
IRB::around_prompt do
value = original_lex(*args, &block)
end
IRB::after_prompt
value
end
end
module IRB
class Irb
alias original_output_value output_value
# Add before_output, after_output, and around_output callbacks
# without damaging original behavior.
def output_value(*args, &block)
IRB::before_output
value = nil
IRB::around_output do
value = original_output_value(*args, &block)
end
IRB::after_output
value
end
end
class Context
alias original_evaluate evaluate
# Add before_eval, after_eval, and around_eval callbacks
# without damaging original behavior.
def evaluate(*args, &block)
IRB::before_eval
value = nil
IRB::around_eval do
value = original_evaluate(*args, &block)
end
IRB::after_eval
value
end
end
end
module IRB
# Called before prompting the user for each line of input. Example:
#
# module IRB
# def self.before_prompt
# puts "(before prompt)"
# end
# end
#
# $ irb
# (before prompt)
# irb(main):009:0> if true
# (before prompt)
# irb(main):010:1> false
# (before prompt)
# irb(main):011:1> end
# => false
#
def self.before_prompt
end
# Called after prompting the user for each line of input.
#
# module IRB
# def self.after_prompt
# puts "(after prompt)"
# end
# end
#
# $ irb
# irb(main):009:0> if true
# (after prompt)
# irb(main):010:1> false
# (after prompt)
# irb(main):011:1> end
# (after prompt)
# => false
#
def self.after_prompt
end
# Called immediately before the prompt. Choose when
# to call the prompt by calling yield. If you don't
# call yield, undefined bahavior will occur.
#
# module IRB
# def self.around_prompt
# puts "(Before prompt)"
# yield
# puts "(After prompt)"
# end
# end
#
# Sample session:
#
# $ irb
# (Before prompt)
# irb(main):001:0> true
# (After prompt)
# => true
#
def self.around_prompt
yield
end
# Called before evaling the user's input.
#
# module IRB
# def self.before_eval
# puts "(Before eval)"
# end
# end
#
# Sample session:
#
# $ irb
# irb(main):001:0> true
# (Before eval)
# => true
#
def self.before_eval
end
# Called after evaling the user's input.
#
# module IRB
# def self.after_eval
# puts "(After eval)"
# end
# end
#
# Sample session:
#
# $ irb
# irb(main):001:0> true
# (After eval)
# => true
#
#
def self.after_eval
end
# Called immediately before evaling the user's input.
# Choose when to fire off the eval by calling yield.
#
# module IRB
# def self.around_eval
# puts "(Before eval)"
# yield
# puts "(After eval)"
# end
# end
#
# Sample session:
#
# $ irb
# irb(main):001:0> true
# (Before eval)
# (After eval)
# => true
#
def self.around_eval
yield
end
# Called before outputing the result of the eval.
#
# module IRB
# def self.before_output
# puts "(Before output)"
# end
# end
#
# Sample session:
#
# $ irb
# irb(main):001:0> true
# (Before output)
# => true
#
def self.before_output
end
# Called after outputing the result of the eval.
#
# module IRB
# def self.after_output
# puts "(After output)"
# end
# end
#
# Sample session:
#
# $ irb
# irb(main):001:0> true
# => true
# (After output)
#
def self.after_output
end
# Called immediately before outputing the result of
# the eval. Choose when to output the results by
# calling yield.
#
# module IRB
# def self.around_output
# puts "(Before output)"
# yield
# puts "(After output)"
# end
# end
#
# Sample session:
#
# $ irb
# irb(main):001:0> true
# (Before output)
# => true
# (After output)
#
def self.around_output
yield
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment