Skip to content

Instantly share code, notes, and snippets.

@iain
iain / README.md
Last active December 12, 2022 00:56
Minimaliain ZSH theme

Minimaliain

This is my ZSH theme. I've had it for years, this adoption is for use as an antigen theme.

The git prompt looks like this:

  • Current git project name (blue: last exit status was ok, red: last exit status failed)
  • Time since last commit
  • Branch name or commit ref (green: working tree is clean, red: dirty working tree)
  • Merge status (↑ is you can push, ↓ is you can pull, ↕ means you have diverged)
@iain
iain / gist:3994958
Created November 1, 2012 16:46
Having fun with Ruby
# encoding: utf-8
class FakeRuby
def if(condition)
puts "if called with #{condition}"
end
def end
puts "end called"
@iain
iain / game_of_life.rb
Created April 30, 2010 00:06
Conway's Game of Life, in one line of Ruby
# Copyright 2010, Iain Hecker. All Rights Reserved
# Conway's Game of Life, in one line of Ruby.
# http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
# Tested and found working on Ruby 1.8.7 and 1.9.2
# The grid is spherical or "wrap around", so the left is connected to the right and top to bottom.
#
# Generate a random grid, 30 cells wide and 10 cells high
#
# grid = "30x10".to_grid
#
@iain
iain / README.markdown
Created June 25, 2011 10:19
RSpec Formatter to load SimpleCov

SpecCoverage formatter

This formatter loads SimpleCov a code coverage reporting tool for Ruby 1.9. SimpleCov already does a good job in making it really easy to configure and load itself, but even good efforts can be improved upon.

The only problem is that you'll need to have something like a spec_helper to load it. You might not have this or you might find it ugly having to resort to environment variables to turn it off or on.

With this formatter, SimpleCov will start, and it will load a .coverage file in which you can add ruby code to configure SimpleCov in a non-obtrusive way. Configuration for a typical Rails app will look like this:

SimpleCov.start 'rails'
@iain
iain / 1_faraday.rb
Created April 22, 2012 12:23
A small sample of Faraday using Etags for caching
require 'logger'
$logger = Logger.new(STDOUT)
require 'active_support/cache'
$cache = ActiveSupport::Cache.lookup_store(:memory_store)
$cache.logger = $logger
$cache.clear
class EtagMiddleware
@iain
iain / gist:8343118
Created January 9, 2014 22:17
instance_eval passes the context as an argument too
class Foo
def call(&block)
instance_eval(&block)
end
end
foo = Foo.new
foo.call do |x|
@iain
iain / cache_helper.rb
Created March 8, 2013 00:23
Example of using eval in a safe way to do something cool and useful.
module CacheHelper
# Caches the block given, and prefixes the cache key with the name of the controller and action.
#
# In JBuilder, the +json+ object is a local variable and to access it, we must pass it to this method.
# Fortunately, this method requires a block, from which we can get the binding, and use that to gain
# access to the json variable. Now we don't have to pass the json variable anymore.
def json_cache(keys, &block)
json = eval("json", block.send(:binding))
json.cache! [params[:controller], params[:action]] + Array.wrap(keys), &block
@iain
iain / gist:4077731
Created November 15, 2012 09:49
A Quick Dependency Injection Example
class GetMessageFromQueue
def initialize(options)
@queue = options.fetch(:queue)
end
def call
@queue.pop
end
require 'mechanize'
require 'yaml'
$results = []
def find(a, link)
a.get link do |page|
page.search(:css, ".post").each do |post|
h2 = post.search(:css, "h2").first
if h2.text =~ /githubber/i
@iain
iain / method_object.rb
Created May 2, 2012 22:29
Hiding methods
module MethodObject
def call(*args, &block)
new(*args, &block).instance_eval { call }
end
def self.extended(cls)
class << cls
private :new
end