Skip to content

Instantly share code, notes, and snippets.

@jamescway
jamescway / gist:9406983
Last active August 29, 2015 13:57
rails 2 utf-8
if Rails::VERSION::MAJOR == 2
require "active_support/core_ext/string/output_safety"
class ERB
module Util
# see https://github.com/rails/rails/issues/7430
def html_escape(s)
s = s.to_s
if s.html_safe?
s
@jamescway
jamescway / gist:8755800
Last active August 29, 2015 13:55
redis startup script
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig: - 85 15
# description: Redis is a persistent key-value database
# processname: redis-server
# config: /etc/redis/redis.conf
# config: /etc/sysconfig/redis
# pidfile: /var/run/redis.pid
@jamescway
jamescway / gist:7729502
Created December 1, 2013 07:21
kata2-erlang
-module(kata2).
-export([chop/0, chop/2]).
% http://stackoverflow.com/questions/1035655/list-is-conceived-as-integer-by-length-function
chop() -> chop(6,[2,4,6,8,10]).
chop(Result) -> io:format("Location index... ~w~n", [Result-1]).
chop(Val, List) ->
if List == [] ->
-1;
@jamescway
jamescway / gist:6208456
Created August 12, 2013 05:37
graph things
# A B C
# B C E
# C G
# D A F
# E F
# F H
require 'rgl/adjacency'
require 'rgl/traversal'
require 'rgl/dot'
require 'test/unit'
@jamescway
jamescway / gist:5770667
Last active December 18, 2015 10:48
Find in nested hashes, prints key path recursively
def search_in_hash(hash, target, args={}, keys='ROOT')
find_key = (args[:find_key] == true)
hash.each do |key, value|
find_target = find_key ? key.to_s : value
if find_target == target
keys = "#{keys} -> '#{key}' => #{target}"
puts keys
keys = ""
end
require 'ruby-debug'
class LineCounter
def initialize
@count = 0
@commenting = false
end
def lines_of_code(file)
@count = 0
@jamescway
jamescway / multi-select-recursive-refactor.rb
Last active December 14, 2015 10:39
multi-select-recursive-refactor.rb
def is_token_prefix?(prefix, tokens)
tokens.each { |token| return true if token.start_with?(prefix) }
false
end
def is_token?(str, tokens)
tokens.each { |token| return true if token == str }
false
end
@jamescway
jamescway / multi-select2.rb
Last active December 14, 2015 10:29
multi-select 2 recursive
def multi_split2(str, delims)
multi(str, delims, [])
end
def multi(str, delims, sum)
return sum if str.empty?
temp, token = "", ""
while !is_token_prefix? str[0], delims
token << str[0]
require 'ruby-debug'
def multi_split(str, tokens)
results = []
buffer = ""
maybe_token = false
temp_str = ""
str.each_char do |c|
if maybe_token
if is_token?(temp_str + c, tokens)
@jamescway
jamescway / gist:3720779
Created September 14, 2012 08:32
Anagram
require File.expand_path(File.join(File.dirname(__FILE__), "bloom.rb"))
class String
def each_char_with_index
0.upto(size - 1) do |index|
yield(self[index..index], index)
end
end
def remove_char_at(index)
return self[1..-1] if index == 0