Yet Another Blog

Rambler, Developer and gamer

Using Lambdas With Let (in Rspec)

| Comments

As many of you already know, let has been a great improvement on declaring instance variables in our before :each blocks. Lazy loading on when it’s used while being kept in memory for the duration of the test. One way in which we use lets, for more complicated variable definitions, is to use let, and provide it with arguments using a lambda:

1
2
3
4
5
let(:user) {
    ->(name, email) {
      double("user", user: email, email: email)
    }
  }

You can then easily setup users in your test:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
describe 'Given a user' do
  context 'with internal address' do
    it 'redirects to intranet' do
      login_as user['Bob', 'bob@internal.com']
      ...
    end
  end
  context 'with external address' do
    let(:staff_user) { user['Bob', 'bob@internal.com'] }
    it 'redirects to external domain' do
      login_as user['Pete', 'pete@gmail.com']
      ...
    end
  end
end

Git Cheat Sheet

| Comments

Git commands that I always forget but find useful:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Recover a file:
git checkout <previous-commit-hash> <file_path>`

# Ignore changes to certain files:
git update-index --assume-unchanged config/database.yml

# Take commit from another branch:
git cherry-pick <commit reference>

# Delete multiple branches starting with some string:
git branch -D `git for-each-ref --format="%(refname:short)" refs/heads/some_string\*`

# List branches in order of which most recent commit
git for-each-ref --sort=-committerdate refs/heads/

I tend to alias things so when on another machine it’s handy!

Resizing Images in Mavericks

| Comments

After a search for batch resizing tools, which most require a bit of fiddling around with. I found a handy command line tool which comes with maverick, sips.

1
sips -Z 1024 *.jpg

If you run the above in a directory of pictures, it will resize all pictures in the current directory. For my needs, I passed -Z to keep the ratio, and a maximum height of 1024 pixels.

As always, make sure to keep the original in case you don’t like the results!