Ruby > irb >
Ruby code snippets >
Table of Contents [hide]
Misc ∞
#shortcut for y model.column_names def show(obj) y(obj.send("column_names")) end
script_console_running = ENV.include?('RAILS_ENV') && IRB.conf[:LOAD_MODULES] && IRB.conf[:LOAD_MODULES].include?('console_with_helpers') rails_running = ENV.include?('RAILS_ENV') && !(IRB.conf[:LOAD_MODULES] && IRB.conf[:LOAD_MODULES].include?('console_with_helpers')) irb_standalone_running = !script_console_running && !rails_running if script_console_running require 'logger' Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT)) end
Dr Nic's ∞
https://web.archive.org/*/drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
2007-03-10
require 'irb/completion' require 'map_by_method' require 'what_methods' require 'pp' IRB.conf[:AUTO_INDENT]=true
what_methods and map_by_method are from gems[doesn't exist] of the same name.
pp
is the pretty printer..
Graeme Mathieson's ∞
http://web.archive.org/web/20071027021158/http://woss.name/dist/irbrc.rb
2007-03-10
Spoiler
# IRB configuration. IRB.conf[:EVAL_HISTORY] = 100 IRB.conf[:SAVE_HISTORY] = 1000 IRB.conf[:HISTORY_FILE] = File::expand_path("~/usr/state/irb_history") IRB.conf[:AUTO_INDENT] = true IRB.conf[:USE_READLINE] = true IRB.conf[:PROMPT_MODE] = :DEFAULT ['irb/completion', 'rubygems', 'stringio'].each do |mod| IRB.conf[:LOAD_MODULES] << mod unless IRB.conf[:LOAD_MODULES].include?(mod) end # Handy functions # Return a list of methods defined locally for a particular object. Useful # for seeing what it does whilst losing all the guff that's implemented # by its parents (eg Object). def local_methods(obj) obj.methods - obj.class.superclass.instance_methods end # List the various methods associated with an object. Inspired by # https://groups.google.com/forum/!topic/comp.lang.ruby/kdfGaSFNqMM def list_methods(obj) inspectee = obj.class == Class ? obj : obj.class c_list = (inspectee.methods - Object.methods).sort i_list = (inspectee.instance_methods - Object.instance_methods).sort a_list = inspectee.class.ancestors puts "Class Methods", "-"*13, c_list.inspect, '' unless c_list.empty? puts "Instance Methods", "-"*16, i_list.inspect, '' unless i_list.empty? puts "Ancestors", "-"*9, a_list.inspect, '' unless a_list.empty? end # MethodFinder, from https://www.nobugs.org/developer/ruby/method_finder.html # which takes an object and an expected result, then finds methods which # match it. Useful for finding out if string concatenation is '+', ',' # or '.', for example... class MethodFinder def initialize( obj, *args ) @obj = obj @args = args end def ==( val ) MethodFinder.show( @obj, val, *@args ) end # Find all methods on [anObject] which, when called with [args] return [expectedResult] def self.find( anObject, expectedResult, *args ) anObject.methods.select { |name| anObject.method(name).arity == args.size }. select { |name| begin anObject.megaClone.method( name ).call(*args) == expectedResult; rescue; end } end # Pretty-prints the results of the previous method def self.show( anObject, expectedResult, *args ) $old_stderr = $stderr; $stderr = StringIO.new methods = find( anObject, expectedResult, *args ).each { |name| print "#{anObject.inspect}.#{name}" print "(" + args.map { |o| o.inspect }.join(", ") + ")" unless args.empty? puts " == #{expectedResult.inspect}" } $stderr = $old_stderr methods end end class Object def what?(*a) MethodFinder.new(self, *a) end # Clone fails on numbers, but they're immutable anyway def megaClone begin self.clone; rescue; self; end end end # Add `ri` support in irb def ri arg puts `ri #{arg}` end class Module def ri(meth=nil) if meth if instance_methods(false).include? meth.to_s puts `ri #{self}##{meth}` end else puts `ri #{self}` end end end def mate *args flattened_args = args.map {|arg| "\"#{arg.to_s}\""}.join ' ' `mate #{flattened_args}` nil end
Last updated 2021-07-10 at 21:27:57