December 2010
18 posts
3 tags
How to connect to a windows file share
It’s quite easy to correct to a windows share from my mac:
smb://ServerName/ShareName
But I always forget the correct syntax for specifying the domain and username:
smb://DOMAIN;User@ServerName/ShareName
And remember to type %20 instead of a space if one exists in the share name.
2 tags
How to bundle bundler →
2 tags
Display ActiveRecord generated SQL queries in the...
rubyquicktips:
If you want the console to display the SQL query that ActiveRecord executes just do the following (before you do anything else in the console):
$ script/console
>> ActiveRecord::Base.logger = Logger.new(STDOUT)
=> #<Logger:0x10322d6d0 ...>
>> User.first
User Load (3.3ms) SELECT * FROM "users" ORDER BY last_name, first_name ASC LIMIT 1
Each call to...
1 tag
Ruby Tracker →
A great way of keeping track of what gems are out-of-date across all your projects.
2 tags
Jeff Kreeftmeijer's blog →
Lots of great (and regular) blog posts
3 tags
attr_accessor_with_default
Here’s a method I hadn’t seen before: attr_accessor_with_default
This ActiveSupport method allows you to set a default value for an attribute accessor:
class Person
attr_accessor_with_default :age, 25
end
some_person.age # => 25
some_person.age = 26
some_person.age # => 26
You can even pass in a block. Thanks to @modsognir for finding this one. I wonder why it...
1 tag
The mighty reflog and the amazing bisect →
I keep forgetting about reflog (thankfully I don’t need it very often) and I totally didn’t get what bisect was before I read this blog post.
3 tags
Give RSpec some Fuubar →
Fuubar is an RSpec formatter that displays a progress bar. Now you can easily see how far into your specs run you are. There’s now also a fuubar for cucumber.
4 tags
fakeldap gem →
This gem lets you create a fake LDAP server that you can use with your tests to stub out calls to a real LDAP server.
1 tag
Fail fast with RSpec
RSpec 2 gives you the ability to fail fast - your specs stop running as soon as an failure has been encountered.
Add this to your spec_helper.rb:
RSpec.configure do |c|
c.fail_fast = true
end
If you don’t want to use fail fast all the time, activate it as required with the command line option:
bundle exec rspec spec --fail-fast
2 tags
Cucumber tip: watch your shoulds
It took me a little while to figure out the problem with this cucumber step:
Then an event exists with code: "1-11", open_date: "2010-11-01", close_date: "2011-01-21", scheduled_date: "2011-02-18", onshore: false
My scenario was passing but why? It should’ve been failing! This is what I meant to write:
Then an event should exist with code: "1-11", open_date: "2010-11-01", close_date:...
2 tags
Bundle the bleeding edge
In developing a new Rails 3 app I’ve regularly come across situations where a bug exists or a feature is missing in the latest official version of a gem. Often a fix is in the works, either in a branch or waiting in a pull request. Sometimes, I’ve made the patch and whilst waiting for my change to be pulled, I need to reference my change. To do this with gem bundler you can modify your...
2 tags
What makes programs hard?
I’m still reading through Martin Fowler’s Refactoring book and am really enjoying. Today I read Kent Beck’s bit about why refactoring works. He listed four reasons why some programs are hard to modify:
hard to read
duplicated logic
additional behaviour requires changes to existing code
complex conditional logic
So when programming, your goal should be to:
make it easy to...
3 tags
The secret to overcoming procrastination
To overcome procrastination, find something harder to do and procrastinate on it instead
Often I procrastinate because the task I’m doing is difficult or detestable. I end up doing something else that is easier or more enjoyable. The secret I’ve discovered for overcoming procrastination, and completing the task at hand, is to work on something that is even more difficult or more...
2 tags
Code Standards →
2 tags
Default hash value
By default, when you try to get the value for a key that does not exist in a Hash you get nil. This can be a pain when all you want is to iterate on the values returned for existing keys. To get around this I set a default value for the hash.
role_mappings = {
'IT' => [:admin, :it],
'Finance' => [:finance],
'HR' => [:hr]
}
role_mappings.default = []
In the case above,...
2 tags
Syntactical shortcut for hashes
Did you know you can rewrite the following hash:
{ :lemon => :yellow, :orange => :orange, :apple => [:red, :green] }
like this:
{ lemon: :yellow, orange: :orange, apple: [:red, :green] }
I don’t think I like this syntactical shortcut and I reckon if I were to use it in any code at work, it would confuse everyone. If the values weren’t symbols then it doesn’t look...
3 tags
Dynamically define methods
The define_method method is a quick way of DRYing up your code and allowing for the creation of dynamic methods. Here’s an example of some code I wrote today where I needed to use define_method:
User.valid_roles.each do |role|
define_method "has_#{role}_role?" do
self.has_role?(role)
end
end
My only dislike of this technique is you can’t simply search for the method name to...
November 2010
10 posts
1 tag
Any fool can write code that a computer can understand. Good programmers write...
– Refactoring: Improving the Design of Existing Code by Martin Fowler