Wed Jan 09 10:44:00 UTC 2008

hacking irb and console for ruby and rails

Posted in Rails at 10:44 AM by matt

Two things you need for your console. Imagine the following

./script/console
>> helper.users_path
"/users"
(then highlight and copy to clipboard the following code.)
>> pastie
(browser opens to a pastie URL of the code you just copied to the clipboard


I also recommend installing wirble so you can have history in your console as well. ( sudo gem install wirble )


To get these helpful commands going in your irb or console session just add the following to your ~/.irbrc file.


# load rubygems and wirble
require 'net/http'
require 'rubygems' rescue nil
require 'wirble'

# load wirble
Wirble.init
Wirble.colorize

def Object.method_added(method)
  return super(method) unless method == :helper
  (class<<self;self;end).send(:remove_method, :method_added)

  def helper(*helper_names)
    returning $helper_proxy ||= Object.new do |helper|
      helper_names.each { |h| helper.extend "#{h}_helper".classify.constantize }
    end
  end

  helper.instance_variable_set("@controller", ActionController::Integration::Session.new)

  def helper.method_missing(method, *args, &block)
    @controller.send(method, *args, &block) if @controller && method.to_s =~ /_path$|_url$/
  end

  helper :application rescue nil
end if ENV['RAILS_ENV']

def pastie
  url = URI.parse("http://pastie.caboo.se/pastes/create")
  parameters = {}
  IO.popen('pbpaste') do |clipboard|
    parameters["paste[body]"] = clipboard.read
  end
  parameters["paste_parser"] = "ruby"
  parameters["paste[authorization]"] = "burger"
  pastie_url = Net::HTTP.post_form(url, parameters).body.match(/href="([^\"]+)"/)[1]
  IO.popen('pbcopy', 'w+') do |clipboard|
    clipboard.write(pastie_url)
  end
  pastie_url
  system("open " + pastie_url)
end

Comments »

  1. Donald said,

    August 9th, 2008 at 07:07 AM

    76974d0962aa012042501b58dc6939ad http://njdokj.info/122ff5460a48f3d16acb8d98fd4704fb/76974d0962aa012042501b58dc6939ad http://njdokj.info/122ff5460a48f3d16acb8d98fd4704fb/76974d0962aa012042501b58dc6939ad [url]http://njdokj.info/122ff5460a48f3d16acb8d98fd4704fb/76974d0962aa012042501b58dc6939ad[url]

Leave a Comment