IRB tools, tricks, and techniques
Utility Belt gives you a ton of new options and techniques in IRB. (You may find yourself putting its language patches in actual projects as well.) The way to use Utility Belt is to edit, or create, your .irbrc file. This is analogous to a Unix .bashrc, .tcshrc, .profile, or similar file.
Windows users have a couple options. The easiest is to define an environment variable called IRBRC and set it to the full path of your irbrc (e.g. "C:\Documents and Settings\
Adding Utility Belt to your .irbrc is easy:
require 'rubygems' require 'utility_belt'
To use the Amazon shortcuts, you also need to set some environment variables. See the section on S3 under "Usage" for more. Also, if your terminal has a light background, you'll need to enable the light-background syntax coloring theme - see the section on themes under "Usage" for more.
Using each of Utility Belt's tools is different:
If your preferred text editor is vi(m), emacs, or TextMate, there are built-in commands: vi, emacs, and mate, respectively. To use another editor, use this command:
edit("the name of my editor")
First copy some code into your clipboard, or put it there with the MacClipboard.write() class method. Then use the command:
or the shortcut version:
Note that this method currently auto-formats all code as Ruby. Support for other languages is possible but not implemented yet. Also, this code is OS X only because it uses the MacClipboard. (A platform-agnostic version without the clipboard aspect is certainly possible.)
Use "h" or "history" to view your command history, and "h! 123" to re-invoke command #123 in your buffer. "h!" with no args will run the last command.
To write your history to a file, use "history_write('/path/to/file')". To edit your history in vi, use "history_to_vi" or its shortcut alias "hvi".
>> grep_classes("ixnum") #=> ["Fixnum"] >> grep_classes("num") #=> ["Bignum", "Fixnum", "Numeric", "REXML::SyncEnumerator"]
>> "any arbitrary string".grep_methods("pretty") #=> ["pretty_inspect", "pretty_print_instance_variables", "pretty_print_inspect", "pretty_print", "pretty_print_cycle"] >> 32.grep_methods("*") #=> ["*", "**"]
At some point it'll probably be a good idea to add regex support for these methods. (Of course if I do that, I'll probably have to change the name from grep to ack.)
regular IRB: "verbose" or "v" / "quiet" or "q"
Rails: "log" method turns verbosity on, there isn't actually any method for turning it off. (Might add one at some point.)
user(:all) / user(1)
User.find(:all) / User.find(1)
Supports arbitrarily complex finds.
First, define your secret keys in your environment variables:
export AMAZON_ACCESS_KEY_ID='foofoofoofoofoo' export AMAZON_SECRET_ACCESS_KEY='barbarbarbarbar'
(Obviously, replace those with actual values.)
Then, you can use
To easily upload public-access files. This method will automatically connect to Amazon S3 for you, if your keys are defined in your environment variables. It also writes the resulting Amazon URL to your clipboard, if you're on OS X, so you can easily share the file with others. Note that you have to create the initial buckets manually - see amazon.rubyforge.org for details on that.
A good way to use this is actually to have a default bucket, and create a further method in your .irbrc file:
def my_bucket_name(filename) aws_upload("my_default_bucket",filename) end
Utility Belt also includes
amazon, a simple command-line uploader for S3. It requires that you set your Amazon environment variables (see above). The usage is:
amazon -b my_bucket_name filename other_filename
(For any arbitrary number of filenames.)
Anytime you want to upload a file to your default bucket on S3, for example, the "images" bucket for your blog - or the "mp3" bucket if you're running a blog like Projectionist - all you have to do is
amazon -b blog_files some_file.name
amazon --bucket blog_files some_file.name
And you're good to go. Of course, if that were too many characters to type, you could always take it one step further with a shell script. For instance, in bash:
alias b1="amazon --bucket my_main_bucket $1" alias b2="amazon --bucket my_other_bucket $1"
And then you would only have to type:
And of course tab completion will handle all the typing of the file name, assuming you choose your file names deliberately ahead of time to make tab completion effective, in which case all you have to type is "b1 f[tab]" and bam.
To pop open a new window in your default browser with Google search results for a given search term, do:
google "any given term"
For one-word terms, you can omit the quotes:
If you already have the search term in your clipboard, you can just type:
(The last approach is useful for unusual command-line error messages.)
Just type as normally. This just enables built-in but underused functionality in IRB. (The IRB default auto-indentation isn't perfect, but it clobbers the living hell out of no indentation at all.)
More activation of built-in but underused functionality in IRB. _ functions like !! in Unix, with one subtle difference. Instead of re-issuing the previous command in your history, it gives you the last result of evaluating a command. This means when you start a new IRB session, _ won't go to your history, it'll just give you nil, and it means that some commands can't be repeated exactly. Nine out of ten times, however, it's pretty much the same thing.
Note that when combined with the h! in the command history code, you basically have two synonyms for !! here. Neither one is an exact replica, but between the two of them, you should have what you need.
Utility Belt incorporates Wirble as a dependency. Wirble provides syntax coloring. Utility Belt allows you to choose between two syntax coloring themes: Wirble's default, and an alternate color set for terminals with light backgrounds. The default theme is enabled by default (duh), but if you have a terminal with a light background, you can enable the light-background theme just by putting the following line of code in your .irbrc:
To revert back to the default, just use this:
To specify an entirely new syntax color scheme, see the source code for this gem or for Wirble.
I use this with RSpec's spec_helper. Instead of:
Spec::Runner.configure do |config| config.use_transactional_fixtures = true config.use_instantiated_fixtures = false config.fixture_path = RAILS_ROOT + '/spec/fixtures' config.mock_with :flexmock end
I do this:
Spec::Runner.configure do |config| with(config) do use_transactional_fixtures = true use_instantiated_fixtures = false fixture_path = RAILS_ROOT + '/spec/fixtures' mock_with :flexmock end end
Note however that this blew up once and I have no idea why. Caveat emptor, et cetera.
Syntactic sugar for functional programming. (More detail here.) Rails' Symbol#to_proc is also included.
(1..3).map(&'*2') => [2, 4, 6] (1..3).map(&'[-1, _, 0]') => [[-1, 1, 0], [-1, 2, 0], [-1, 3, 0]] (1..3).map(&'x -> y -> x * y').map(&'') => [2, 4, 6] (1..5).select(&'>2') => [3, 4, 5] (1..3).map(&'x -> y -> x * y').map(&'.call(2)') => [2, 4, 6] .map(&"(1.._).inject(&'*')") => 
>> .is_an? Array #=> true
Most people don't care, but personally, "is_a? Array" drives me completely insane.
if !response.incomplete? && !response.invalid? && response.total > 0
if response.not.incomplete? && response.not.invalid? && response.total > 0
Syntactic sugar for boolean conditions. (More detail here.)
Because life is too short to type whole words.