Skip to content

Commit

Permalink
Initial public checkin.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed Feb 12, 2010
0 parents commit 2204bbc
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
support
*.rbc
9 changes: 9 additions & 0 deletions 00-rubygems.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby
# -*- ruby -*-

# Load RubyGems if it is available.
begin
# require 'rubygems'
rescue LoadError
puts "(no rubygems)"
end
24 changes: 24 additions & 0 deletions 01-usage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env ruby
# -*- ruby -*-

unless Object.const_defined?("HELP")
Object.const_set("HELP", [])
end

def usage(method_name=nil, comment=nil)
if method_name.nil?
width = HELP.collect { |pair| pair[0].size }.max
HELP.sort.each do |name, desc|
printf "%-#{width}s -- %s\n", name, desc
end
elsif comment.nil?
puts "Usage: usage 'method_name', 'comment'"
else
HELP << [method_name, comment]
end
nil
end

usage "h", "Display help"
alias h usage

10 changes: 10 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
= Ruby/IRB setup

Here is my personal setup for IRB. Put these files in a .irb.d directory in your home directory. Then link .irbrc to the dot.irbrc file.

cd
ln -s .irb.d/dot.irbrc .irbrc

Enjoy ...

-- Jim Weirich
4 changes: 4 additions & 0 deletions do_data_setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def demo
puts "Setting up demo data"
require "data_setup"
end
9 changes: 9 additions & 0 deletions dot.irbrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby
# -*- ruby -*-

Dir["#{ENV['HOME']}/.irb.d/**/*.rb"].sort.each do |fn|
next if fn =~ /_test.rb$/
load fn
end

noecho
11 changes: 11 additions & 0 deletions eigenclass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Object
def eigenclass
class << self; self; end
end
end

class Class
def metaclass
eigenclass
end
end
27 changes: 27 additions & 0 deletions exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Exception
def self.children
@children ||= []
end
end

def exceptions
ObjectSpace.each_object(Class) do |klass|
next if klass.to_s =~ /^(IRB|RubyLex|Errno)/
next unless klass.ancestors.include?(Exception)
begin
klass.superclass.children << klass unless klass == Exception
rescue NoMethodError => ex
puts "For #{klass}: #{ex.message}"
end
end
show_exceptions(Exception)
end

def show_exceptions(root, indent=0)
puts "#{' '*indent}#{root}"
root.children.each do |child|
show_exceptions(child, indent + 1)
end
nil
end

32 changes: 32 additions & 0 deletions no_echo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env ruby
# -*- ruby -*-

usage "clear", "Clear the Screen"
def clear
system "clear"
end
alias :cls :clear

usage "noecho", "Do not echo characters as typed (for emacs shell mode)."
def noecho
puts `stty -echo`
end

usage "echo", "Echo characters as typed."
def echo
puts `stty echo`
end

def in_emacs_shell?
ENV['TERM'] == 'dumb'
end

def emacs_noecho
noecho if in_emacs_shell?
end

#emacs_noecho

END {
emacs_noecho
}
12 changes: 12 additions & 0 deletions parsing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if RUBY_VERSION >= "1.9"
require 'ripper'
require 'pp'

def parse(string)
sexp = Ripper::SexpBuilder.new(string).parse
sexp = sexp[1] if sexp.first == :program
sexp = sexp[2] if sexp.first == :stmts_add
pp sexp
nil
end
end
70 changes: 70 additions & 0 deletions pretty_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env ruby
# -*- ruby -*-

unless Object.const_defined?("BASE_METHODS")
Object.const_set("BASE_METHODS", Object.new.methods)
end

def find_method(method_name)
result = []
# method_name = Regexp.new(Regexp.quote(method_name.to_s)) if method_name.kind_of?(Symbol)
method_name = Regexp.new(method_name) if method_name.kind_of?(String)
ObjectSpace.each_object(Class) do |klass|
methods = klass.instance_methods.select { |name| name =~ method_name }
methods.each do |name|
result << "#{klass}##{name}"
end
end
puts result.sort
end

def pretty_list(list, all=:shallow)
list = (list - BASE_METHODS) if all == :shallow
list = list.select { |m| m.to_s =~ all } if all.kind_of?(Regexp)
categories = Hash.new { |h, k| h[k] = [] }
list.sort.each do |m|
if m =~ /^[a-zA-Z]/
categories[m[0,1]] << m
else
categories['@'] << m
end
end
categories.keys.sort.each do |k|
puts " #{categories[k].join(' ')}"
end
nil
end

def pretty_methods(obj, all=false)
methods = obj.methods
case obj
when Class
methods -= Class.methods unless all
end
pretty_list(obj.methods, all)
end

def pretty_class_methods(obj, all=false)
case obj
when Class
pretty_list(obj.methods, all)
else
pretty_list(obj.class.methods, all)
end
end

def pretty_instance_methods(klass, all=false)
pretty_list(klass.instance_methods, all)
end

usage "m", "Pretty print all the methods of an object"
alias :m :pretty_methods

usage "cm", "Pretty print all the class methods of an object."
alias :cm :pretty_class_methods

usage "im", "Pretty print all the instance methods of a class object."
alias :im :pretty_instance_methods

usage "fm", "Find method named NAME."
alias :fm :find_method
6 changes: 6 additions & 0 deletions prompt_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby
# -*- ruby -*-

IRB.conf[:PROMPT_MODE] = :SIMPLE
#IRB.conf[:PROMPT_MODE] = :INF_RUBY
IRB.conf[:AUTO_INDENT] = true
32 changes: 32 additions & 0 deletions quiet_mode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env ruby
# -*- ruby -*-

usage "--", "Print the last value"
def __
p IRB.CurrentContext.last_value
IRB.CurrentContext.echo ? nil : IRB.CurrentContext.last_value
end

def set_echo_mode(is_echo)
if IRB.CurrentContext && IRB.CurrentContext.respond_to?(:echo)
IRB.CurrentContext.echo = is_echo
else
IRB.conf[:ECHO] = is_echo
end
end

def quiet
set_echo_mode(false)
end

def not_quiet
set_echo_mode(true)
end

usage "q", "Enter 'quiet' mode."
alias q quiet

usage "nq", "Exit 'quiet' mode."
alias nq not_quiet

not_quiet
64 changes: 64 additions & 0 deletions rails_aid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
usage "session", "get the session object for a session id"
def session(session_id=nil)
fail "Rails Session not loaded" unless
defined? CGI::Session::ActiveRecordStore::Session

if session_id
@session_id = session_id
else
session_id = @session_id
end
fail "Need a Session ID" unless session_id
session = CGI::Session::ActiveRecordStore::Session.find_by_session_id(session_id)
eval %{
def session.to_s
"<Session@#{session_id}>"
end
def session.decode
safe_marshal_load(self['data'].unpack('m').first)
end
}
def session.inspect
to_s
end
session
end

usage "safe_marshal_load", "Load the marshalled data safely"
def safe_marshal_load(marsh_data)
missing_constants_seen = {}
begin
Marshal.load(marsh_data)
rescue ArgumentError => ex
if ex.message =~ /^undefined class\/module (.+)/
class_name = $1
logger.info("Marshalled class not found: #{class_name}, autoloading and trying again")
unless missing_constants_seen[class_name]
missing_constants_seen[class_name] = true
class_name.constantize rescue nil
retry
end
end
end
end

class HistoryMonitor
def initialize(session_id)
@session_id = session_id
end
def show
s = session(@session_id)
s.decode[:history_stack].show
end
end

def hm(id=nil)
if id
@monitor = HistoryMonitor.new(id)
end
if @monitor.nil?
fail "Need session id"
end
@monitor.show
end

3 changes: 3 additions & 0 deletions space_flight.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def odds(percent, bucks)
20 * (percent/100.0) * bucks
end
8 changes: 8 additions & 0 deletions timer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Simple timer for benchmarks
def tm()
start = Time.now
result = yield
delta = Time.now - start
puts "Elapsed: #{delta} seconds"
result
end
5 changes: 5 additions & 0 deletions use_lib.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
# -*- ruby -*-

$:.unshift('lib')

0 comments on commit 2204bbc

Please sign in to comment.