Skip to content

Instantly share code, notes, and snippets.

class BinaryIndexedTree
def initialize(n)
@freq = Array.new(n, 0)
@btree = Array.new(n + 1, 0)
end
def sum(index)
sum = 0
index += 1
class BrailleReader
def read(braille)
# Convert [[O. .O O. O.], [.. OO OO O.], [.O .O .. OO]] [] to [O....O .OOO.O O.OO.. O.O.OO]
lines = ([] << braille[0].split(' ') << braille[1].split(' ') << braille[2].split(' '))
chars = lines[0].zip(lines[1], lines[2]).map {|ch| ch.join('')}
char_reader = BrailleCharReader.new
chars.map {|ch| char_reader.decipher(ch)}.join
end
end
@miwest929
miwest929 / exceptional_examples.rb
Last active December 21, 2015 19:19
Code examples from my Ruby Exception Handling presentation: http://slid.es/mwest-mdsol/being-exceptional-at-ruby
# Code for Exception Anti-Pattern #1: blah rescue nil
require 'time'
class DoSomeWork
# Bad: Fatal exceptions are swallowed up.
def self.milliseconds(ms)
Time.now.iso8601(ms) rescue nil
end
# Good: Specific about which exception we care about
@miwest929
miwest929 / gist:3931661
Created October 22, 2012 14:06
scripts that pretty-prints all JSON in api scenarios
require 'json'
module PrettyPrintJSON
CUCUMBER_JSON_DELIMETER = '"""'
def self.from_file(filename)
in_json_block = false
json_block = ""
formatted_file = ""
@miwest929
miwest929 / gist:3854127
Created October 8, 2012 18:40
test correctness of messaging_apps refactor + performance benchmark
require 'set'
namespace :query do
task :validate => :environment do
users = User.all
differences = 0
failed_users = []
users.each do |u|
old_apps = old_way u
new_apps = u.messaging_apps