Skip to content

Instantly share code, notes, and snippets.

View psychocandy's full-sized avatar

Amir Friedman psychocandy

  • Berlin, Germany
View GitHub Profile
@psychocandy
psychocandy / .profile
Created December 21, 2011 05:08
My mac bash profile
# /etc/bash/bashrc
#
# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output. So make sure this doesn't display
# anything or bad things will happen !
# Test for an interactive shell. There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
@psychocandy
psychocandy / test.rb
Created July 13, 2012 19:51 — forked from Constellation/test.rb
Extracting id from a crx package
require 'crxmake'
require 'digest/sha2'
# generate id from pem
def generate_id pkey
key = CrxMake::KEY + pkey.public_key.to_der
id = Digest::SHA256.hexdigest(key)
id[0..32].split('').map{|ch| ('a'.ord + ch.hex).chr }.join('')
end
@psychocandy
psychocandy / URI-monkey-patch.rb
Created July 17, 2012 16:11
Fix ArgumentError invalid %-encoding for malformed URLs
# The following should prevent an ArgumentError "invalid %-encoding (...%)" exception from being raised for malformed URLs.
# To use this code simply drop this in your rails app initializers.
# See: https://github.com/rack/rack/issues/337
# Taken from: http://stackoverflow.com/a/11162317/1075006
module URI
major, minor, patch = RUBY_VERSION.split('.').map { |v| v.to_i }
# lib/stop_words.rb:
module StopWords
def stop_words_finder
stop_words = ["ich", "heute"]
end
end
# models/pool.rb:
@psychocandy
psychocandy / mock_tire.rb
Created October 18, 2012 10:58 — forked from dvdplm/mock_tire.rb
Mocking helper to stop Tire from submitting searches to Elasticsearch
module Tire
module Disable
module ClassMethods
def mock_es_response_doc
@mock_es_response_doc ||=
'{"took": 1,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 0,"max_score": null,"hits": []}}'
end
def enable! &blk
old_enabled = @tire_enabled || false
@psychocandy
psychocandy / evil_algorithm.rb
Created December 13, 2012 11:06
Ruby program which finds the biggest prime factors for a number.
def generate(n)
return [] if n == 1
factor = (2..n).find { |x| n % x == 0 }
[factor] + generate(n / factor)
end
@psychocandy
psychocandy / git-unmerged
Last active December 12, 2015 00:08 — forked from antirez/gist:4554824
#!/usr/bin/tclsh8.5
#
# Install: clone and copy to somewhere in your $PATH, e.g:
# git clone https://gist.github.com/4681074.git git-unmerged && cd git-unmerged && chmod +x git-unmerged && sudo mv git-unmerged /usr/bin && cd .. && rm -rf git-unmerged
# Usage: git-unmerged branch1 branch2
#
# Shows all the non-common commits in the two branches, where non-common
# commits means simply commits with a unique commit *message*.
proc getlog branch {
@psychocandy
psychocandy / enum.rb
Last active December 12, 2015 09:59
A fair implementation of an enum in Ruby. Inspired by Elad Meidar's article on gistflow - http://gistflow.com/posts/682-ruby-enums-approaches
# usage:
# FRUITS = Enum.new(:apples, :oranges)
# FRUITS[:apples] # => 0
# FRUITS[0] # => :apples
class Enum < Hash
def initialize(*members)
super()
@reverse = {}
@psychocandy
psychocandy / map_hash.rb
Created February 17, 2013 11:17
An idea for mapping to a hash. Kudos to @dorkalev.
# [1,2,3].map_hash { |x| [x, x*2] }
# => {1=>2, 2=>4, 3=>6}
# { foo: "bar", fiz: "baz" }.map_hash { |k,v| [v,k] }
# => {"bar"=>:foo, "baz"=>:fiz}
module Enumerable
def map_hash
Hash[*self.inject([]) { |arr, a| arr += yield(a) }]
end
end
@psychocandy
psychocandy / autovivify_hash.rb
Created February 17, 2013 13:57
Autovivification which allows you to nest away and save you a lot of nil? checks.
hash = Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
hash[:foo]["bar"][:baz] = 13
# => {:foo=>{"bar"=>{:baz=>13}}}