RSS
 

Archive for the ‘ruby’ Category

A handy vim macro for RSpec users

18 Feb

In this screencast, I teach how to record a useful vim macro that does a simple RSpec “refactoring”.

(You’ll want to open it on Youtube and watch in high-resolution.)

Commands used:
7G – jump to line 7
“td – delete into the t register
“tp – put from the t register
qq – start recording a macro (just a series of keystrokes) into register q
@q – execute the keystrokes stored in register q
vmap – set up a mapping active only when in visual mode

 
1 Comment

Posted in ruby, vim

 

How to Land Your First Rails Patch (Screencast)

14 Feb

Last week I posted How to land your first patch in Rails.

The post was well-received, but one commenter asked if I might put together more-detailed steps on how to make your first docpatch. Well, I’ve done just that, and the result is the screencast below.

It’s only 4 minutes, but contains everything you need to know to get your name on the Rails Contributors list. Check it out!

Update: in the screencast, I mention that you need to message lifo for commit access to docrails. However, as Xavier noted below, docrails now has public write access for everyone, so need to perform that step.

By the way, if you liked the video, you should check out my other screencasts for Rails programmers.

 
1 Comment

Posted in rails, ruby

 

A handy ruby trick: Class.new

01 Feb

Today I learned that you can pass a block to Class.new, like so:

klass = Class.new do
  def self.speak
    "hi!"
  end
end

>> klass.speak
=> "hi!

This is a handy for creating one-off classes; the kind you’ll throw away quickly.

It’s likely that you’d do this during tests. Here’s a test in active_support that uses this technique:

class ClassAttributeAccessorTest < Test::Unit::TestCase
  def setup
    @class = Class.new do
      cattr_accessor :foo
      cattr_accessor :bar,  :instance_writer => false
      cattr_reader   :shaq, :instance_reader => false
    end

    # Now grab an instance of our new class
    @object = @class.new
  end
 
  # Elided: tests that make sure the cattr_accessor method behaves

end

Another thing I picked up at the same time: you can pass a constant to Class.new and it will set that as the superclass:

>> klass = Class.new(String)
=> #<Class:0x105604658>
>> klass.superclass
=> String

Here’s another test from Rails. This one makes use of both tricks I just showed you:

  test "configuration is crystalizeable" do
    parent = Class.new { include ActiveSupport::Configurable }
    child  = Class.new(parent)

    # ...
  end
 
No Comments

Posted in rails, ruby