Skip to content

Instantly share code, notes, and snippets.

💂
I may be slow to respond.

iain iain

View GitHub Profile
@iain
iain / README.md
Last active Mar 12, 2017
Minimaliain ZSH theme
View README.md

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)
View ackermann.clj
(defn ack [m n]
(if (= m 0)
(+ n 1)
(if (= n 0)
(ack (- m 1) 1)
(ack (- m 1) (ack m (- n 1))))))
View auto-align.vim
" Automatically align multiline hashes in Ruby (Ruby 1.9 syntax only).
function! AlignRubyHashes()
" Only look for lines that start with a simple keyword, colon, space, and
" ending in a comma.
let p = '^\s*\w\+:\s.*,$'
" Don't do anything if there is no Tabularize, or there is no surrounding
" line that can be aligned with.
if exists(':Tabularize') && (getline(line('.')) =~# '^\s*\w\+:\s$') && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
" save current position because later command moves the cursor
@iain
iain / gist:8343118
Created Jan 9, 2014
instance_eval passes the context as an argument too
View gist:8343118
class Foo
def call(&block)
instance_eval(&block)
end
end
foo = Foo.new
foo.call do |x|
@iain
iain / cache_helper.rb
Created Mar 8, 2013
Example of using eval in a safe way to do something cool and useful.
View cache_helper.rb
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 Nov 15, 2012
A Quick Dependency Injection Example
View gist:4077731
class GetMessageFromQueue
def initialize(options)
@queue = options.fetch(:queue)
end
def call
@queue.pop
end
@iain
iain / gist:3994958
Created Nov 1, 2012
Having fun with Ruby
View gist:3994958
# encoding: utf-8
class FakeRuby
def if(condition)
puts "if called with #{condition}"
end
def end
puts "end called"
View githubber.rb
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
View method_object.rb
module MethodObject
def call(*args, &block)
new(*args, &block).instance_eval { call }
end
def self.extended(cls)
class << cls
private :new
end
View publishes_post.rb
class PublishesPost
def self.call(*args)
new(*args).call
end
attr_reader :post, :user
def initialize(post, user)
@post, @user = post, user
You can’t perform that action at this time.