Good notes posted by Olly
RSS feedWriting and reading a cookie in the same request.
As of 0349278f3da9f7f532330cf295eed35ede3bae66 cookie updates will persist in the current request.
Testing an options hash receives certain parameters
This method is very useful for testing methods that use the ruby idiom of accepting a hash with configurable options.
class Example def self.find(options = {}) ... end end
We can use hash_including to ensure that certain options are passed in when mocking it.
Example.should_receive(:find).with(hash_including(:conditions => 'some conditions')) Example.find(:conditions => 'some_conditions', :order => 1) # => Passes expectation Example.find(:order => 1) # => Fails expectation
This can also be used to great effect with the anything matcher. For example:
hash_including(:key => anything) hash_including(anything => 'value')
Convert an Array of Arrays to a Hash using inject
Converting an array of arrays to a hash using inject:
array = [['A', 'a'], ['B', 'b'], ['C', 'c']] hash = array.inject({}) do |memo, values| memo[values.first] = values.last memo end hash # => {'A' => 'a', 'B' => 'b', 'C' => 'c'}
Using validates_format_of to validate URIs
You can use the validates_format_of with the regular expression in the URI library ensure valid URIs.
Example
require 'uri' class Feed < ActiveRecord::Base validates_format_of :uri, :with => URI.regexp end