Skip to content

Instantly share code, notes, and snippets.

Node(1)
Node(2)
Node(3)
Node(4)
Node(5)
Node(6)
Node(7)
Node(8)
class Object
def self.enumerable *meths
meths.each do |meth|
alias_method "#{meth}_without_enumerator", meth
class_eval %{
def #{meth}(*args, &block)
return Enumerable::Enumerator.new(self, #{meth.inspect}, *args, &block) unless block_given?
#{meth}_without_enumerator(*args, &block)
end
}
@epitron
epitron / rle.rb
Created April 1, 2011 00:18
runlength encode/decode binary strings
s = "1100100001100001110111101110110011111010010000100101011110010110"
def rle(s)
s.gsub(/(0{3,9}|1{3,9})/) { "#{$1.size}#{$1.chars.first}" }
end
def rld(s)
s.gsub(/([3-9])([01])/) { $2 * $1.to_i }
end
[02:04 PM] epi@fizz :: ~/code/pry $ ./go
(in /home/epi/code/pry)
NOTE: Gem::Specification#has_rdoc= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#has_rdoc= called from /home/epi/code/pry/Rakefile:26
.
NOTE: Gem::Specification#has_rdoc= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#has_rdoc= called from /home/epi/code/pry/Rakefile:26
.
NOTE: Gem::Specification#has_rdoc= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#has_rdoc= called from /home/epi/code/pry/Rakefile:26
command "require", "Require gem(s)" do |*gems|
gems = gems.join(' ').gsub(',', '').split(/\s+/)
gems.each do |gem|
begin
if require gem
output.puts "#{gem.yellow} loaded"
else
output.puts "#{gem.white} already loaded"
end
rescue LoadError => e
@epitron
epitron / gems-command.rb
Last active September 25, 2015 14:28
A "gems" command for Pry
command "gems", "List/search installed gems. (Optional parameter: a regexp to limit the search)" do |arg|
gems = Gem.source_index.gems.values.group_by(&:name)
if arg
query = Regexp.new(arg, Regexp::IGNORECASE)
gems = gems.select { |gemname, specs| gemname =~ query }
end
gems.each do |gemname, specs|
versions = specs.map(&:version).sort.reverse.map(&:to_s)
versions = [bright_green(versions.first)] +
@epitron
epitron / plugin_example.rb
Created April 22, 2011 07:02
possible pry plugin parameterization
#
# The result is an anonymous module (named "name") that can be mixed into the
# Commands class.
#
Pry.plugin "name" do
## Settings
settings do
bool :make_awesome, :default => true
#
# Creates a new CommandSet and automatically mixes it into Pry::Commands.
#
Pry.plugin :test do
settings do
bool :make_awesome, :default => true
enum :goat_type, :default => "plain", :options => %w[ plain mountain billy ]
string :favorite_goat, :default => "Super Dave the Goat", :validate => proc { goat[/super/i] }
int :expire_goats, :default => 5.minutes
@epitron
epitron / pry-fancy_requre.rb
Last active September 25, 2015 15:37
A require command that also prints out all the modules/classes that were loaded into Ruby!
def req(mod)
puts " |_ #{mod}"
require mod
yield if block_given?
rescue Exception => e
p e
end
## Fancy Require w/ Modules
require 'curses'
def init_screen
Curses.noecho # do not show typed keys
Curses.init_screen
Curses.start_color
Curses.stdscr.keypad(true) # enable arrow keys
Curses.curs_set(0)
Curses.ESCDELAY = 0