This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Abbreviator | |
def self.abbreviate(names) | |
abbrs = names.sort.inject({}) do |hash, name| | |
hash[name] = replace_non_word_chars(name).slice(0,3) | |
hash | |
end | |
Hash[*abbrs.map do |name, abbr| | |
[name, | |
(abbrs.values.select{|v|v.eql?(abbr)}.size == 1) ? abbr : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
end | |
possible_matches = possible_matches.map {|c,i| s2_chars.slice(i,s1.length).join} | |
match = possible_matches.any? {|candidate| candidate == s1 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# yields an 11 digit number in base-36 | |
# with an equal probability distribution for all digits | |
(0..10).to_a.map {(rand*36).to_i.to_s(36)}.join | |
# this is slightly preferable to the more simple | |
(rand*(36**11)).to_i.to_s(36) | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://en.wikipedia.org/wiki/Go_%28programming_language%29 | |
https://blog.engineyard.com/2014/intro-to-go-rubyists | |
http://www.sitepoint.com/go-rubyists/ | |
http://www.sitepoint.com/go-rubyists-ii/ | |
http://gofullstack.com/articles/go-project-structure-for-rubyists.html | |
http://techjaw.com/2015/02/04/go-for-rubyists-martini-and-revel/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func = -> (node, func) { | |
node.children.each do |node| | |
# do something | |
func.call(node, func) | |
end | |
} | |
func.call(root, func) |