Skip to content

Instantly share code, notes, and snippets.

View roryokane's full-sized avatar

Rory O’Kane roryokane

View GitHub Profile
@roryokane
roryokane / examples.rb
Created July 31, 2012 19:56
Hash#fetch_recursively for Ruby
# encoding: utf-8
# examples:
nested_hashes = {:a=>{:b=>{:c=>{:more=>"answer"}}}, :num=>42}
path = [:a, :b, :c, :more]
nested_hashes.fetch_recursively(path) #=> "answer"
nested_hashes.fetch_recursively([:num]) #=> 42
# wrong:
nested_hashes.fetch_recursively(:a) # raises NoMethodError; should pass an Array
nested_hashes.fetch_recursively([:num, :something]) # raises NoMethodError; can’t fetch from 42
@roryokane
roryokane / symbol_access_hash.rb
Created July 31, 2012 19:58
Hash with symbol access of Strings in Ruby – the start of an implementation
# encoding: utf-8
class HashWithSymbolAccessOfStrings < Hash
def [](key)
begin
self.fetch key
rescue KeyError
string_key = key.to_s
if string_key != key
self[string_version]
@roryokane
roryokane / examples.rb
Created August 1, 2012 18:32
Object#apply_repeatedly and #send_repeatedly for Ruby
# encoding: utf-8
3 * 2 * 2 * 2 * 2 #=> 48
3 * (2 ** 4) #=> 48
3.send_repeatedly(4, :*, 2) #=> 48
3.apply_repeatedly(4) { |obj| obj * 2 } #=> 48
BasicObject.singleton_class.superclass # => Class
BasicObject.singleton_class.superclass.superclass.superclass.superclass #=> BasicObject
BasicObject.singleton_class.send_repeatedly(4, :superclass) #=> BasicObject
@roryokane
roryokane / implementation_in_class.rb
Created August 3, 2012 17:51 — forked from jcasimir/gist:3247167
refactoring posted code by creating Enumerable#find_mapped
LOOKUP_CHAIN = [:params, :user, :session, :http, :default]
def self.set_by(inputs)
LOOKUP_CHAIN.find_mapped do |lookup|
locale = send("by_#{lookup}", inputs[lookup])
end
end
@roryokane
roryokane / original_with_test_harness.rb
Created August 3, 2012 17:57 — forked from jcasimir/gist:3247167
test harness for posted code wanting refactoring
class LocaleChooser
LOOKUP_CHAIN = [:params, :user, :session, :http, :default]
def self.set_by(inputs)
locale = nil
LOOKUP_CHAIN.each do |lookup|
locale = send("by_#{lookup}", inputs[lookup])
break if locale
html {zoom:1.32;}
img, a[href=news] {zoom:.76;}
/* choose one: */
body * {font-family:'Helvetica Neue', Helvetica, 'Liberation Sans', sans-serif !important;}
/* body * {font-family:Palatino, 'Liberation Serif', Georgia, serif !important;} */
a[href=news] {font-family:Palatino, 'Liberation Serif', Georgia, serif !important;}
code {font:10px/1.5 Menlo, monospace !important;}
@roryokane
roryokane / .rbenv-version
Created September 4, 2012 12:31
editing Wikipedia ISBN calculation code
1.9.3-p194
@roryokane
roryokane / Blank page with single-color background.html
Created September 7, 2012 18:50
Blank page with single-color background
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Blank page with single-color background</title>
<style>
body {
color: black;
background-color: black;

I had a pretty elaborate vim setup https://github.com/mbriggs/dotvim, and now I am using emacs with evil. I’ll go through the reasons why, with a giant caveat being I actually don’t care what editor anyone who reads this uses, so long as they learn it and it works for them. As someone who has used both, vim shines for people who aren’t in to heavy customization. You can heavily customize vim, but it is so easy to make it dog slow, and even with a ton of work you won’t hit what you can accomplish in emacs. Here are some examples, and also essentially why I am no longer using vim, YMMV

If I want to run a command and have it pipe to another buffer AND not completely lock up the editor, I can do that with a few lines and compilation-mode. This is next to impossible in vim. I can split my editor window and have a shell running on the other side, that i can use all the keys and tools on that I use to edit code, again, not possible in vim. I can have a repl connected to my editor that the editor uses for auto-compl

@roryokane
roryokane / Hide_GitHub_Project_Page_Latest_Commit.user.js
Created September 11, 2012 19:44
UserScript: Hide GitHub Project Page Latest Commit
// ==UserScript==
// @name Hide GitHub Project Page Latest Commit
// @namespace roryokane
// @include /^https?://github\.com/[^/]+/[^/]+/?$/
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js
// ==/UserScript==
// GreaseMonkey doesn't fire on pushState and popState right now,
// so the box is reshown and not rehidden when going forward and back to the page again
// TODO work around – at least make toggle link recognize when it’s been thwarted