Technical
irbrc

I’ve started moving towards “rsheling” so following can be inputed into ~/.irbrc on nix based systems or read for windows in order to automate some tasks in ./script/console

Get attributes ( fields ) of the Model


def attributes_for(class_name)
   puts class_name.find_first.attributes.keys.sort
end

#Or you could simply do
Person.column_names # Found much later then the attributes_for

#returns all the attributes of the model

Get Help


def ri(*name)
  system(%{ri #{name.map{|name| name.to_s}.join(" ")}})
end

Get Stats ( sweet stats :))


def rake_stats system( %{rake stats} )
end

Get methods of the class, object or whatever else you’re inspecting without object’s instance methods ( Thanks to Geoff and Jacob Harris)


# >> "a".self_methods
# => ["%", "*", "+",...]
class Object
  def self_methods
    (self.methods - Object.instance_methods).sort
  end
end
# >> "a".methods.length
# => 216
# >> "a".self_methods.length
# => 141

Run ruby commands or files from console

This one would still need some love since its taking params as a string
if you know a way of changing it please feel free to update.


def run(*name)
  system(%{ ruby #{name} })
end
# Run single integration file
>> run "test/integration/authentication_test.rb" 
Loaded suite test/integration/authentication_test.rb
Started
.........
Finished in 4.805332 seconds.

9 tests, 57 assertions, 0 failures, 0 errors
=> true
# Get ruby version
>> run "-v" 
ruby 1.8.4 (2005-12-24) [i686-linux]
=> true

Run functional or unit tests from console

I am using autotest, nevertheless I found that sometimes it good to run tests individually. Following will let you to brows through all your test files based on your choice(:functional,:unit)

Its much more convenient and batter to run rake tests, but some times you just wanted to run each test individually, also its very unfortunate that its a luxury to have a separate test environment.


def test(*type)
  if ENV['RAILS_ENV']  'test'
   Dir.entries("test/#{type.to_s}").each do |file|
     run "test/#{type.to_s}/#{file}" if file.last(3)  ”.rb” 
   end
  else
  puts “You dont want to run your tests in environment other then ‘test’” 
  end
end #Not that im sending symbol
test :functional #Loaded suite test/functional/parent_contact_controller_test #Started #............. #Finished in 2.839345 seconds.
test :unit #Loaded suite test/unit/legal_entity_exception_test #Started #. #Finished in 0.339246 seconds.

How To inspect helpers from console

Originally I found it on err.the_blog written by PJ Hyett and Chris Wanstrath. – Thank You Im posting it here in case if it will be deleted from original place

in case of any concerns from people mentioned above I promise to remove this peace.


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']

or download from http://pastie.caboo.se/27125/text

Geoff’s Contribution(s):


# an irb clear
def clear
  STDOUT.write("#{0x1b.chr}[0;0H")
  STDOUT.write("#{0x1b.chr}[2J")
end
# or ctrl+l (lower l on *nix)
# ctrl+k on mac

# Method searching
#   >> [1,2,3].method_search("sort")
#   => ["sort", "sort!", "sort_by"]
class Object
  def method_search(string)
    self.methods.find_all{ |i| i.match(/#{string}/) }.sort
  end
end

# Tab-completion of methods as seen here: http://showmedo.com/videos/video?name=rubyLakeIrbCompletion
require 'irb/completion'

Auto-tabbing turned on : as found on http://drnicwilliams.com/category/ruby/gems/


#Add in ~/.irbrc
IRB.conf[:AUTO_INDENT]=true

So now if you write itereator, method or anything that would require indent, you would see


>> [1,2].each do |n|
?>     puts n
>>   end
1
2
=> [1, 2]

Some “svnning”


#>> svn :up
#At revision 626.
#=> true
def svn(*action)
  system %(/home/tpdev/bin/linux/svn  #{action})
end

Historify your session by Mike Clark


HISTFILE = "~/.irb.hist" unless defined? HISTFILE
MAXHISTSIZE = 100 unless defined? MAXHISTSIZE

begin
  if defined? Readline::HISTORY
    histfile = File::expand_path(HISTFILE)
    if File::exists?(histfile)
      lines = IO::readlines(histfile).collect { |line| line.chomp }
      Readline::HISTORY.push(*lines)
    end

    Kernel::at_exit {
      lines = Readline::HISTORY.to_a.reverse.uniq.reverse
      lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.nitems > MAXHISTSIZE
      File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) {|f|
        lines.each {|line| f.puts line }
      }
    }
  end
end