Skip to content

Instantly share code, notes, and snippets.

@bgswan
bgswan / x-rays.md
Last active May 31, 2018 14:20
Software X-Rays for Ruby

Software Design X-rays

  1. Measure interest rates. Ruby code that changed most frequently in the last year. git log --format=format: --name-only --after="2017-05-18" -- "*.rb" | egrep -v '^$' | sort | uniq -c | sort -r | head -10

  2. Measure size of each file. Lines of code is a simple proxy for complexity. Just Ruby files cloc . --by-file --csv --include-lang=Ruby --quiet

  3. Combine the two into a single output of filename, change_frequency, size.

@bgswan
bgswan / flatten.rb
Created April 22, 2016 02:27
Example of flattening an Array.
module ArrayHelper
# Returns a new Array which is the result
# flattening any nested arrays.
#
# @param [Array] the array to flatten
# @param [Array] the array to append results to.
#
# @return [Array] the flattened array.
#
@bgswan
bgswan / mailing_list_spec.rb
Last active December 24, 2015 15:19
Alternative version of test described here http://re-factor.com/blog/2013/09/27/slow-tests-are-the-symptom-not-the-cause/ using domain model rather than "service object"
describe MailingList do
let(:notifications) { double('notifications') }
let(:user) { User.new }
it 'registers a new user' do
expect(notifications).to receive(:call).with(user, 'list_name')
mailing_list = MailingList.new('list_name', notifications)
mailing_list.add(user)
class Analytics
ConnectionFailure = Class.new(StandardError)
def self.sync(account)
new(Analytics::Client.new(CC.config[:analytics_api_key])).sync(account)
end
def initialize(analytics_client)
@analytics_client = analytics_client
end
@bgswan
bgswan / mock_abuse.java
Created August 21, 2012 19:42
Pathalogical Mock Example
JMSMockObjectFactory objectFactory = new JMSMockObjectFactory();
JMSTestModule testModule = new JMSTestModule(objectFactory);
TopicConnectionFactory beTopicConnectionFactory = objectFactory.createMockTopicConnectionFactory();
Topic beTopic = objectFactory.getDestinationManager().createTopic("sampleTopic");
//Perform you test - Publish the message
//Create the expected Message by the subscriber
MockMessage mockmessage = new MockMessage();
//set properties to mockmessage
//condition - similar to assertequals
testModule.verifyReceivedTopicMessageEquals("sampleTopic", 1, mockmessage);
@bgswan
bgswan / config.rb
Created April 7, 2011 10:44
Scottish Ruby Conference tutorial exercise
class AppServerConfiguration
attr_accessor :port, :admin_password
end
class Configuration
attr_accessor :tail_logs, :max_connections, :admin_password
def initialize(block)
block.call(self)
end
def counter(start=0, increment=1)
lambda do
initial = start
start += increment
initial
end
end
result = counter(10, 2)
def counter(start=0, increment=1)
lambda do
puts start
start += increment
end
end
result = counter(10, 2)
result.call # 10
@bgswan
bgswan / cart_steps.rb
Created February 24, 2011 17:35
The step definitions for the cart feature
When /^(\d+) x \$(\d+)/ do |qty, price|
@basket ||= []
@basket << (qty.to_i * price.to_i)
end
Then /^Total bill = \$(.+)$/ do |expected_total|
@basket.inject(0) {|memo, item| memo += item}.should eql(expected_total.to_i)
end
@bgswan
bgswan / cart.feature
Created February 24, 2011 17:34
A cucumber feature without the noise
Feature: A simple shopping basket
Scenario: adding items
* 1 x $15 screw-driver
* 2 x $12 lamp
* Total bill = $39