RubyGeneral
From Rory.wiki
Contents |
Ruby Debugging
tutorial Very handy for examining problems in complex apps (eg metasploit) per the tuorial require 'ruby-debug' at the top of the source file and place the keyword debugger in the file just before the error.
Then use p command to evaluate statements (or pp for a more structured output) , var local shows the local variables and their values. another tutorial on ruby-debug
Ruby Error Handling
general syntax is
begin statements that might need rescuing rescue <error_class> => variable_name puts variable_name.message end
Timeout Module
Useful for rescuing tasks that might be long running and fail (eg mechanize jobs on unreliable websites) module docs
general syntax is
begin variable_name = Timeout::timeout(timeout_in_seconds) {code_which_might_timeout} rescue Timeout::Error some_nice_rescue_stuff_here end
classes and calling from command line
If you've got a class in a file and want to be able to call if from irb and also the command line
for the script lines to instantiate the class wrap them in
class MyClass def initialize some_stuff_here end def run some_more_stuff_here end end if __FILE__ == $0 object = MyClass.new object.run end
Running system commands from ruby scripts
From here and here. If you're running system programs from the OS and you need to process the results the best way is
%x[<system_command>]
if you need to insert variables into the command use #{<variable>} . If you need to capture STDERR from the command as well as STDOUT use
%x[<system_command> 2>&1]
irb
- It's worth noting that if there's a problem with loading .irbrc (for example a missing gem file) then it'll fail silently by default (well on ubuntu anyway). So if your methods/colouring aren't working all of a sudden it's worth investigating by checking the code being loaded in there.
.irbrc
colorization is provided by wirble (you need to install the gem first)
require 'irb/completion' require 'rubygems' require 'wirble' colors = Wirble::Colorize.colors.merge({ :object_class => :black, :class => :dark_grey, :symbol => :red, :symbol_prefix => :blue, }) Wirble::Colorize.colors = colors Wirble.init Wirble.colorize ARGV.concat [ "--readline", "--prompt-mode", "simple"] class Object def mymethods (self.methods - Object.new.methods).sort end end
quieting IRB return values
if you commonly work with large text files in IRB, then reading them in can be a pain as the return value from the .read or .readlines method is huge. You can restrict the size of the return value displayed in irb as mentioned here
conf.return_format = "=> %.512s\n"
where 512 is the number of characters to display
Links
Stack Overflow Post on irb tricks
Metaprogramming Stuff
Mainly from here
Finding a list of the public instance methods for a class (with the noise of the base Object class removed)
<ClassName>.public_instance_methods - Object.public_instance_methods
Showing what classes a given class inherits from
<ClassName>.ancestors
using Class.constants you can get a list of contants provided by the class.
Resources and Links
General
Ruby switcher Easy switching of various ruby versions on a single host.
Useful list of regular expressions for things like IP addresses.
command line app. skeleton Notes on setting up a basic command-line application skeleton with usage and options parsing
pleac ruby file examples Many example of ruby file usage
Makeing ruby 1.9.1 default on Ubuntu
Tutorial on Ruby File and Directory Handling
