Return to Snippet

Revision: 7341
at July 18, 2008 12:23 by charlesroper


Updated Code
# Charles Roper's custom .irbrc for Windows

# References:
# http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
# http://pablotron.org/software/wirble/
# http://pablotron.org/software/wirble/README
# http://eigenclass.org/hiki/fastri#l3
# http://eigenclass.org/hiki/irb+ri+completion
# http://mislav.caboo.se/rails/faster-ri-documentation/
# http://programblings.com/2007/10/18/jruby-not-in-its-setting/

require 'rubygems'
require 'win32/console/ansi'  # required for colorisation
require 'irb/completion'      # [tab][tab]
require 'map_by_method'       # requires map_by_method gem (http://snipr.com/30q36)
require 'what_methods'        # requires the what_methods gem
require 'pp'
require 'wirble'              # http://pablotron.org/software/wirble/

IRB.conf[:AUTO_INDENT]  = true
IRB.conf[:USE_READLINE] = true

ENV['IRB_HISTORY_FILE'] = "%USERPROFILE%\\\\.irb_history"

#start wirble (with color)
Wirble.init
Wirble.colorize

# FastRI stuff (requires fastri gem)

# could also use fri instead of qri
# note that Windows requires the .bat
FASTRI = "qri.bat"

module Kernel
  def r(arg)
    puts `#{FASTRI} "#{arg}"`
  end
  private :r
end

class Object
  def puts_ri_documentation_for(obj, meth)
    case self
    when Module
      candidates = ancestors.map{|klass| "#{klass}::#{meth}"}
      candidates.concat(class << self; ancestors end.map{|k| "#{k}##{meth}"})
    else
      candidates = self.class.ancestors.map{|klass|  "#{klass}##{meth}"}
    end
    candidates.each do |candidate|
      #puts "TRYING #{candidate}"
      desc = `#{FASTRI} '#{candidate}'`
      unless desc.chomp == "nil"
      # uncomment to use ri (and some patience)
      #desc = `ri -T '#{candidate}' 2>/dev/null`
      #unless desc.empty?
        puts desc
        return true
      end
    end
    false
  end
  private :puts_ri_documentation_for

  def method_missing(meth, *args, &block)
    if md = /ri_(.*)/.match(meth.to_s)
      unless puts_ri_documentation_for(self,md[1])
        "Ri doesn't know about ##{meth}"
      end
    else
      super
    end
  end

  def ri_(meth)
    unless puts_ri_documentation_for(self,meth.to_s)
      "Ri doesn't know about ##{meth}"
    end
  end
end

RICompletionProc = proc{|input|
  bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
  case input
  when /(\s*(.*)\.ri_)(.*)/
    pre = $1
    receiver = $2
    meth = $3 ? /\A#{Regexp.quote($3)}/ : /./ #}
    begin
      candidates = eval("#{receiver}.methods", bind).map do |m|
        case m
        when /[A-Za-z_]/; m
        else # needs escaping
          %{"#{m}"}
        end
      end
      candidates = candidates.grep(meth)
      candidates.map{|s| pre + s }
    rescue Exception
      candidates = []
    end
  when /([A-Z]\w+)#(\w*)/ #}
    klass = $1
    meth = $2 ? /\A#{Regexp.quote($2)}/ : /./
    candidates = eval("#{klass}.instance_methods(false)", bind)
    candidates = candidates.grep(meth)
    candidates.map{|s| "'" + klass + '#' + s + "'"}
  else
    IRB::InputCompletor::CompletionProc.call(input)
  end
}
#Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
Readline.basic_word_break_characters= " \t\n\\><=;|&"
Readline.completion_proc = RICompletionProc

class Object
  # 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).
  # See comments here: http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
  def local_methods(obj = self)
    obj.methods(false).sort
  end
end

puts ".irbrc successfully loaded"

Revision: 7340
at July 18, 2008 11:59 by charlesroper


Initial Code
# Charles Roper's custom .irbrc for Windows

# References:
# http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
# http://pablotron.org/software/wirble/
# http://pablotron.org/software/wirble/README
# http://eigenclass.org/hiki/fastri#l3
# http://eigenclass.org/hiki/irb+ri+completion
# http://mislav.caboo.se/rails/faster-ri-documentation/
# http://programblings.com/2007/10/18/jruby-not-in-its-setting/

require 'rubygems'
require 'win32/console/ansi'  # required for colorisation
require 'irb/completion'      # [tab][tab]
require 'map_by_method'       # requires map_by_method gem (http://snipr.com/30q36)
require 'what_methods'        # requires the what_methods gem
require 'pp'
require 'wirble'              # http://pablotron.org/software/wirble/

IRB.conf[:AUTO_INDENT]  = true
IRB.conf[:USE_READLINE] = true

ENV['IRB_HISTORY_FILE'] = "%USERPROFILE%\\\\.irb_history"

#start wirble (with color)
Wirble.init
Wirble.colorize

# FastRI stuff (requires fastri gem)

# could also use fri instead of qri
# note that Windows requires the .bat
FASTRI = "qri"

module Kernel
  def r(arg)
    puts `#{FASTRI} "#{arg}"`
  end
  private :r
end

class Object
  def puts_ri_documentation_for(obj, meth)
    case self
    when Module
      candidates = ancestors.map{|klass| "#{klass}::#{meth}"}
      candidates.concat(class << self; ancestors end.map{|k| "#{k}##{meth}"})
    else
      candidates = self.class.ancestors.map{|klass|  "#{klass}##{meth}"}
    end
    candidates.each do |candidate|
      #puts "TRYING #{candidate}"
      desc = `#{FASTRI} '#{candidate}'`
      unless desc.chomp == "nil"
      # uncomment to use ri (and some patience)
      #desc = `ri -T '#{candidate}' 2>/dev/null`
      #unless desc.empty?
        puts desc
        return true
      end
    end
    false
  end
  private :puts_ri_documentation_for

  def method_missing(meth, *args, &block)
    if md = /ri_(.*)/.match(meth.to_s)
      unless puts_ri_documentation_for(self,md[1])
        "Ri doesn't know about ##{meth}"
      end
    else
      super
    end
  end

  def ri_(meth)
    unless puts_ri_documentation_for(self,meth.to_s)
      "Ri doesn't know about ##{meth}"
    end
  end
end

RICompletionProc = proc{|input|
  bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
  case input
  when /(\s*(.*)\.ri_)(.*)/
    pre = $1
    receiver = $2
    meth = $3 ? /\A#{Regexp.quote($3)}/ : /./ #}
    begin
      candidates = eval("#{receiver}.methods", bind).map do |m|
        case m
        when /[A-Za-z_]/; m
        else # needs escaping
          %{"#{m}"}
        end
      end
      candidates = candidates.grep(meth)
      candidates.map{|s| pre + s }
    rescue Exception
      candidates = []
    end
  when /([A-Z]\w+)#(\w*)/ #}
    klass = $1
    meth = $2 ? /\A#{Regexp.quote($2)}/ : /./
    candidates = eval("#{klass}.instance_methods(false)", bind)
    candidates = candidates.grep(meth)
    candidates.map{|s| "'" + klass + '#' + s + "'"}
  else
    IRB::InputCompletor::CompletionProc.call(input)
  end
}
#Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
Readline.basic_word_break_characters= " \t\n\\><=;|&"
Readline.completion_proc = RICompletionProc

class Object
  # 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).
  # See comments here: http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
  def local_methods(obj = self)
    obj.methods(false).sort
  end
end

puts ".irbrc successfully loaded"

Initial URL

                                

Initial Description
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++.

3. Install the following gems:

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

map_by_method 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/

Initial Title
Charles Roper's custom .irbrc for Windows

Initial Tags
ruby

Initial Language
Ruby