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)
(defn ack [m n]
(if (= m 0)
(+ n 1)
(if (= n 0)
(ack (- m 1) 1)
(ack (- m 1) (ack m (- n 1))))))
" 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 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
@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"
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
class PublishesPost
def self.call(*args)
new(*args).call
end
attr_reader :post, :user
def initialize(post, user)
@post, @user = post, user