Tuesday, August 18, 2009

Search and Replace in VIM

Syntax

:s/search/replace/
:[range]s/search/replace/

Examples

# Search & replace in current line
:s/entry/interview

# Search & replace in lines 8 to 10
:8,10 s/search/replace/

# Global search & replace
:%s/entry/interview/g

# Global search & replace with prompt
:%s/entry/interview/gc

More awesome search and replace tips at http://linux.com/learn/tutorials/8255-vim-tips-the-basics-of-search-and-replace

Breaking Cucumber feature tests into separate smaller files

Previous I had the following Cucumber feature tests in a single file

# features/webrat/edit_enrollment_table

@edit_enrollment_details
Feature: Editing of enrollment details from enrollment table
In order to edit enrollment details easily
As an admin
I want to edit enrollment details from enrollment table

Scenario: Admin should be able to enter entry test date
....
Scenario: Admin should be able to edit entry test date
...
Scenario: Admin should be able to enter entry test status
...
Scenario: Admin should be able to edit entry test status
...
Scenario: Admin should be able to enter interview test date
...
Scenario: Admin should be able to edit interview test date
...
Scenario: Admin should be able to enter interview test status
...
Scenario: Admin should be able to edit interview test status
...

Pretty massive. Why not split them up into manageable bits?

# features/webrat/edit_entry_test_details

@edit_enrollment_details
@edit_entry_test_details
Feature: Editing of entry test details from enrollment table
In order to edit entry test details easily
As an admin
I want to edit entry test details from enrollment table

Scenario: Admin should be able to enter entry test date
...
Scenario: Admin should be able to edit entry test date
...
Scenario: Admin should be able to enter entry test status
...
Scenario: Admin should be able to edit entry test status
...

# features/webrat/edit_interview_test_details

@edit_enrollment_details
@edit_interview_test_details
Feature: Editing of interview test details from enrollment table
In order to edit interview test details easily
As an admin
I want to edit interview test details from enrollment table

Scenario: Admin should be able to enter interview test date
...
Scenario: Admin should be able to edit interview test date
...
Scenario: Admin should be able to enter interview test status
...
Scenario: Admin should be able to edit interview test status
...

So here's what I think the advantages are:

- Cleaner code in a single file. Well, at least not so chunky!
- Running small number of tests is much FASTER,
- Can still run full quite with proper tagging. ie. @edit_enrollment_details

Calendar Date Select gem for Rails


sudo gem install "calendar_date_select"

# config/environment.rb
config.gem "calendar_date_select"

# app/views/layouts/application.erb
<%= calendar_date_select_includes 'silver' %>

# app/views/assessments/_form.html.erb
<%= calendar_date_select_tag 'assessment[schedule]', Time.now, :time => true, :id => 'assessment_schedule' %>

Project homepage: http://code.google.com/p/calendardateselect/
Demos: http://electronicholas.com/calendar

Sunday, August 16, 2009

Reload current file in VIM


# To reload current file in Vim
:edit or :e

# To reload and discard modifications
:edit! or :e!

# To autoload every time file gets modified
:set autoread

Bluetooth tethering your iPhone to Ubuntu (works on multiple machines too)

1. Check out your iPhone bluetooth address

hcitool scan

2. Edit "/etc/default/bluetooth"

PAND_ENABLED=1
PAND_OPTIONS="--listen --role=PANU --devup /etc/bluetooth/pan/dev-up --devdown /etc/bluetooth/pan/dev-down"

3. Create directory "/etc/bluetooth/pan"

4. Create file "/etc/bluetooth/pan/dev-up"

#!/bin/bash
ifup bnep0

5. Create file "/etc/bluetooth/pan/dev-down"

#!/bin/bash
ifdown bnep0

6. Install pand & create PAN connection

sudo apt-get install bluez-compat
sudo pand -c IPHONE BLUETOOTH ADDRESS

7. Assign IP address to iPhone

sudo dhclient3 bnep0

8. To disconnect tethering

sudo pand -K

You can use the same method to tethering to multiple machines at the same time.

Thursday, August 13, 2009

Common regular expresssions








Taken from http://net.tutsplus.com/tutorials/other/8-regular-expressions-you-should-know/

Cucumber > 0.3.7 freezes hash table and changes loading of shoulda


diff --git a/config/environments/test.rb b/config/environments/test.rb

-config.gem "thoughtbot-shoulda", :lib => "shoulda/rails", :source => "http://gems.github.com"
+config.gem "thoughtbot-shoulda", :lib => "shoulda", :source => "http://gems.github.com"


diff --git a/features/step_definitions/enrollment_steps.rb b/features/step_definitions/enrollment_steps.rb

- enrollments.hashes.each do |hash|
+ enrollments.hashes.each do |h|
+ hash = h.dup # Hash is frozen by cucumber. Not sure why.