Skip to content

Instantly share code, notes, and snippets.

#
# An Array-like class that's used for a very specific purpose: the `out[]` array
# in Pry. MRU stands for most-recently-used.)
#
# It acts like an array with a maximum size -- if you try to add more elements than
# the maximum, the elements at the beginning of the array are discarded.
#
# Also, it keeps track of which entries are most recently accessed, and throws
# out only the oldest entries.
#
@epitron
epitron / sample-plugin.rb
Created January 10, 2012 03:28
An example of a non-class-based way of sticking metadata onto a command.
Pry.plugin("something") do
command "amazing" do
...
end.help do
short "blah"
long "A long description."
end
command /more-amazing!*/ do
@epitron
epitron / hash-sets.rb
Created January 18, 2012 18:29
Hash with set operations
#
# The original idea was to use set operations to help dealing with a method's "options" hash.
#
# However, since I can't override || and &&, I wasn't able to figure out reasonable semantics.
#
# Therefore, I implemented `with_defaults`, which is what you want most of the time, and then
# wrote some thoughts about how set operations might work.
#
class Hash
@epitron
epitron / ansi2html.rb
Created January 28, 2012 10:02
Convert ANSI to HTML
require 'epitools'
class State
attr_accessor :fore, :back, :attrs
COLORS = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
ATTRS = {
0 => :reset,
@epitron
epitron / blackcarpet.rb
Created January 30, 2012 22:30
BlackCarpet (ANSI renderer for RedCarpet)
require 'epitools'
require 'redcarpet'
require 'coderay'
def indented?(text)
indent_sizes = text.lines.map{ |line| if line =~ /^(\s+)/ then $1 else '' end }.map(&:size)
indent_sizes.all? {|dent| dent > 0 }
end
def unwrap(text)
@epitron
epitron / gist:1872524
Created February 21, 2012 00:23
cirwin's WTF command
Pry.commands.command(/^wtf([?!]*)/, "show backtrace") do |arg|
raise Pry::CommandError, "No most-recent exception" unless _pry_.last_exception
output.puts _pry_.last_exception
output.puts _pry_.last_exception.backtrace.first([arg.size, 0.5].max * 10)
end
@epitron
epitron / pry-ri.rb
Created April 13, 2012 08:42
Faster RI, with Pry's pager.
Pry.commands.command "ri", "RI it up!" do |*names|
# lazy loading
require 'rdoc/ri/driver'
unless RDoc::RI::Driver.responds_to? :monkeypatched?
class RDoc::RI::Driver
def page
lesspipe {|less| yield less}
end
@epitron
epitron / bench
Created September 10, 2012 06:35
Faster 'require' method
echo -n "with: "
time ruby -I. -rfast-require -e 'require "epitools"'
echo -n "without: "
time ruby -e 'require "epitools"'
@epitron
epitron / spec_helper.rb
Created September 15, 2012 18:11
How to use pry-rescue in your specs.
require 'spork'
Spork.prefork do
require 'rspec'
require 'pry'
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
@epitron
epitron / _table_of_package_items.rhtml
Created September 17, 2012 03:56
Demo of "easy_table()" (a DSL for turning a collection of Ruby objects into an HTML table), created for a Rails app in 2006.
<%= easy_table(items, :class=>"table-style1", :tbody_id=>@table_id) do
col("Name") {|o| o.drug_price.name}
col("Qty", :quantity)
col("Price") {|o| number_to_currency(o.price) }
col("Actions") do |o|
link_to image_tag('small/delete.png'),
:action => "return_package_item",
:package_item => o.id,