Posted By

charlesroper on 07/18/08


Tagged

ruby irbrc


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

noah
webstic


Charles Roper's custom .irbrc for Windows


 / Published in: Ruby
 

  1. Make sure the following environment variable is set:

    WinXP: HOME=C:\Documents and Settings*username*

    Vista: HOME=C:\Users*username*

  2. Create an .irbrc file in %HOME%:

    $ cd %HOME% $ touch .irbrc

If you don't have touch installed, use a good text editor to save the .irbrc. E Text Editor is good, as is Notepad++.

  1. Install the following gems:

fastri [http://eigenclass.org/hiki.rb?cmd=view&p=fastri&key=fastri]

mapbymethod and what_methods [http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/]

wirble [http://pablotron.org/software/wirble/]

win32console http://rubyforge.org/projects/winconsole/

  1. # Charles Roper's custom .irbrc for Windows
  2.  
  3. # References:
  4. # http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
  5. # http://pablotron.org/software/wirble/
  6. # http://pablotron.org/software/wirble/README
  7. # http://eigenclass.org/hiki/fastri#l3
  8. # http://eigenclass.org/hiki/irb+ri+completion
  9. # http://mislav.caboo.se/rails/faster-ri-documentation/
  10. # http://programblings.com/2007/10/18/jruby-not-in-its-setting/
  11.  
  12. require 'rubygems'
  13. require 'win32/console/ansi' # required for colorisation
  14. require 'irb/completion' # [tab][tab]
  15. require 'map_by_method' # requires map_by_method gem (http://snipr.com/30q36)
  16. require 'what_methods' # requires the what_methods gem
  17. require 'pp'
  18. require 'wirble' # http://pablotron.org/software/wirble/
  19.  
  20. IRB.conf[:AUTO_INDENT] = true
  21. IRB.conf[:USE_READLINE] = true
  22.  
  23. ENV['IRB_HISTORY_FILE'] = "%USERPROFILE%\\\\.irb_history"
  24.  
  25. #start wirble (with color)
  26. Wirble.init
  27. Wirble.colorize
  28.  
  29. # FastRI stuff (requires fastri gem)
  30.  
  31. # could also use fri instead of qri
  32. # note that Windows requires the .bat
  33. FASTRI = "qri.bat"
  34.  
  35. module Kernel
  36. def r(arg)
  37. puts `#{FASTRI} "#{arg}"`
  38. end
  39. private :r
  40. end
  41.  
  42. class Object
  43. def puts_ri_documentation_for(obj, meth)
  44. case self
  45. when Module
  46. candidates = ancestors.map{|klass| "#{klass}::#{meth}"}
  47. candidates.concat(class << self; ancestors end.map{|k| "#{k}##{meth}"})
  48. else
  49. candidates = self.class.ancestors.map{|klass| "#{klass}##{meth}"}
  50. end
  51. candidates.each do |candidate|
  52. #puts "TRYING #{candidate}"
  53. desc = `#{FASTRI} '#{candidate}'`
  54. unless desc.chomp == "nil"
  55. # uncomment to use ri (and some patience)
  56. #desc = `ri -T '#{candidate}' 2>/dev/null`
  57. #unless desc.empty?
  58. puts desc
  59. return true
  60. end
  61. end
  62. false
  63. end
  64. private :puts_ri_documentation_for
  65.  
  66. def method_missing(meth, *args, &block)
  67. if md = /ri_(.*)/.match(meth.to_s)
  68. unless puts_ri_documentation_for(self,md[1])
  69. "Ri doesn't know about ##{meth}"
  70. end
  71. else
  72. super
  73. end
  74. end
  75.  
  76. def ri_(meth)
  77. unless puts_ri_documentation_for(self,meth.to_s)
  78. "Ri doesn't know about ##{meth}"
  79. end
  80. end
  81. end
  82.  
  83. RICompletionProc = proc{|input|
  84. bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
  85. case input
  86. when /(\s*(.*)\.ri_)(.*)/
  87. pre = $1
  88. receiver = $2
  89. meth = $3 ? /\A#{Regexp.quote($3)}/ : /./ #}
  90. begin
  91. candidates = eval("#{receiver}.methods", bind).map do |m|
  92. case m
  93. when /[A-Za-z_]/; m
  94. else # needs escaping
  95. %{"#{m}"}
  96. end
  97. end
  98. candidates = candidates.grep(meth)
  99. candidates.map{|s| pre + s }
  100. rescue Exception
  101. candidates = []
  102. end
  103. when /([A-Z]\w+)#(\w*)/ #}
  104. klass = $1
  105. meth = $2 ? /\A#{Regexp.quote($2)}/ : /./
  106. candidates = eval("#{klass}.instance_methods(false)", bind)
  107. candidates = candidates.grep(meth)
  108. candidates.map{|s| "'" + klass + '#' + s + "'"}
  109. else
  110. IRB::InputCompletor::CompletionProc.call(input)
  111. end
  112. }
  113. #Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
  114. Readline.basic_word_break_characters= " \t\n\\><=;|&"
  115. Readline.completion_proc = RICompletionProc
  116.  
  117. class Object
  118. # Return a list of methods defined locally for a particular object. Useful
  119. # for seeing what it does whilst losing all the guff that's implemented
  120. # by its parents (eg Object).
  121. # See comments here: http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
  122. def local_methods(obj = self)
  123. obj.methods(false).sort
  124. end
  125. end
  126.  
  127. puts ".irbrc successfully loaded"

Report this snippet  

You need to login to post a comment.