Skip to content

Instantly share code, notes, and snippets.

View stalcottsmith's full-sized avatar

Steven Talcott Smith stalcottsmith

View GitHub Profile
stalcottsmith / recursive function
Created June 13, 2015 03:51
Is there an anonymous recursion alternative in Ruby? This is the best I could come up with which requires naming.
func = -> (node, func) {
node.children.each do |node|
# do something, func)
}, func)
stalcottsmith / GoLangLinks
Last active August 29, 2015 14:19
Quick study links for reviewing GoLang with an eye toward web development
stalcottsmith / gist:1b74155cbfe3a23d8573
Last active August 29, 2015 14:04
My go-to solution for random id numbers which programmers frequently need
# yields an 11 digit number in base-36
# with an equal probability distribution for all digits
(0..10) {(rand*36).to_i.to_s(36)}.join
# this is slightly preferable to the more simple
# because any number so generated will occasionally be "short"
# and because the first digit will follow a non-random distribution
# in terms of frequency due to some strange number theory thing
stalcottsmith / gist:9481995
Created March 11, 2014 09:01
quick naive is_substring implementation
def is_substring?(s1, s2)
match = false
if s2.length > s1.length
s1_chars, s2_chars = s1.split(''), s2.split('')
possible_matches = []
s2_chars.each_with_index do |c, i|
possible_matches << [c, i] if c == s1_chars.first
possible_matches = {|c,i| s2_chars.slice(i,s1.length).join}
match = possible_matches.any? {|candidate| candidate == s1 }
stalcottsmith / role.rb
Created March 3, 2011 09:44
Declarative Auth Role Ancestor hierarchy
class Role < ActiveRecord::Base
require 'declarative_authorization/development_support/analyzer'
has_many :assignments
has_many :users, :through => :assignments
validates :name, :presence => true
validates :name, :uniqueness => true
def ancestors
module Abbreviator
def self.abbreviate(names)
abbrs = names.sort.inject({}) do |hash, name|
hash[name] = replace_non_word_chars(name).slice(0,3)
Hash[* do |name, abbr|
({|v|v.eql?(abbr)}.size == 1) ? abbr :