noizZze

TextMate: Execute Ruby Code

In my day-to-day development I keep IRB handy just to sketch some statement or check how some method works with a given data set. It’s all quick and helpful, but working in TextMate, I always forget about it also has a tiny small feature that lets us do the same in much more comfortable fashion. The next time I need to check something, I will:

  • Open an empty text document
  • Set type to Ruby with ⌃⌥⇧R
  • Enter my ruby code just like this:
    1
    2
    3
    4
    5
    
    a = [1, 2, 3]
    b = [2, 3]
    a + b # =>
    
    a - b # =>
    
  • Finally, run it with ⌃⌘⇧E and get this as the result:
    1
    2
    3
    4
    5
    
    a = [1, 2, 3]
    b = [2, 3]
    a + b # => [1, 2, 3, 2, 3]
    
    a - b # => [1]
    

See? Handy.

By the way, I may have noticed that this format is frequently used in Ruby docs. Now you know how do they do it, right?