Skip to content

Instantly share code, notes, and snippets.

View jamesmartin's full-sized avatar
🤫
Shhh

James Martin jamesmartin

🤫
Shhh
View GitHub Profile
class SomeController
def some_action
@list_of_partials = [] << "say_hello" << "ask_about_their_day" << "say_goodbye"
respond_to do |format|
format.js { render :update_some_page_section }
end
end
end
@jamesmartin
jamesmartin / foo.rb
Created July 12, 2012 14:31
Calling protected instance methods from a class method
class Foo
def self.bar(instance = new)
instance.send(:bar)
end
protected
def bar
"protected!"
end
end
@jamesmartin
jamesmartin / remove_duplicates_spec.rb
Created July 18, 2012 13:25
A solution to the problem posed in a Job application for Alpha Sights
# From:
# http://www.alphasights.com/apply/ruby-developer-london
# The rules:
# Ordering in the input is not defined. Ordering in the result is not important.
# Do not rely on the names of the hash keys other than :time.
# The field below executes your code in a Sandbox with $SAFE=4,
# so you can't define new classes, use global variables, etc.
def remove_duplicates(log)
group_by_date(log).map do |time, entries|
@jamesmartin
jamesmartin / poor_logger.rb
Created July 19, 2012 13:18
Poor man's logger
class Foo
def initialize(options={})
@logger = options[:logger] || Proc.new { }
end
def do_stuff
@logger.call "Logged!"
end
end
@jamesmartin
jamesmartin / postCode.java
Created August 17, 2012 15:59
PostCode TDD Example
public class PostCode {
private static final String DIGIT_ALPHA_ALPHA = "\\d[A-Z][A-Z]";
private static final int OUTCODE_LENGTH = 3;
private String inCode;
private String outCode;
public PostCode(String rawPostCode) {
String postCode = removeSpacesAndUpperCase(rawPostCode);
inCode = postCode.substring(0, inCodeLengthFrom(postCode));
outCode = postCode.substring(inCodeLengthFrom(postCode));
@jamesmartin
jamesmartin / big_test_files.sh
Created October 1, 2012 15:39
Find test files that have a lot of "should" expectations
find test -iname "*_test.rb" | xargs grep -c "should" | sort -n -k2 -t: | grep -v "helper"
@jamesmartin
jamesmartin / timetests.sh
Created October 2, 2012 14:21
Run each test file separately and report the time it took
#!/bin/bash
test_files() {
find test -iname "*_test.rb" | xargs grep -l "should" | grep -v "helper" | grep -v "performance" | grep -v "browser"
}
time_command() {
local cmd=$*
TIMEFORMAT="%3R"
(time $cmd > /dev/null) 2>&1
}
@jamesmartin
jamesmartin / multiassign.rb
Created October 12, 2012 16:21
Multiple assignment in Ruby
baz = true
foo, bar, bat = if self.baz?
puts "this gets executed" #=> But puts returns nil, which gets assigned to foo, bar and bat
else
puts "this doesn't get executed"
end
class Parent < ActiveRecord::Base
has_many :children
validates :name, presence: true
end
class Child < ActiveRecord::Base
belongs_to :parent
end
@jamesmartin
jamesmartin / suppress.rb
Created October 24, 2012 20:56
Demonstration of temporarily suppressing, or changing, the definition of a method on a given object instance
class Suppress
def initialize method
@method_name = method.to_sym
end
def self.method_named method
new method
end
def on_object object, &block