Skip to content

Instantly share code, notes, and snippets.

@camertron
camertron / ruby-cldr-export.rb
Created May 24, 2012 23:48
Exporting base CLDR data using Sven Fuch's ruby-cldr gem
require 'rubygems'
require 'ruby-cldr'
require 'cldr'
require 'cldr/data'
require 'cldr/download'
require 'yaml'
# NOTE: nb = no (norwegian)
locales = ["he", "en", "fr", "it", "de", "es", "ja", "ko", "ru", "tr", "pt", "fil", "hi", "ms", "id", "nl", "da", "sv", "pl", "hu", "fi", "nb", "zh", "zh-Hant", "ar", "fa", "ur", "th", "uk", "ca", "el", "af", "cs", "eu"]
components = ["calendars", "currencies", "delimiters", "languages", "numbers", "Plurals", "territories", "timezones", "units"]
@camertron
camertron / measure.rb
Created June 15, 2012 22:48
Measure the memory taken by a Ruby object (by Robert Klemme)
#!/bin/env ruby
# lazy hack from Robert Klemme
module Memory
# sizes are guessed, I was too lazy to look
# them up and then they are also platform
# dependent
REF_SIZE = 4 # ?
OBJ_OVERHEAD = 4 # ?
@camertron
camertron / custom_units_yml.rb
Created July 28, 2012 21:23
Creating custom units.yml from Twitter Translation Center
require 'rubygems'
require 'json'
require 'ya2yaml'
require 'twitter_cldr'
require 'fileutils'
ENDPOINT = "http://translate.twttr.com/api/2/twitter/phrase/{{phrase_id}}/translations.json"
merged = { :day => 19636, :hour => 19638, :second => 19639, :minute => 19634 }.inject({}) do |final_ret, (label, phrase_id)|
cur_data = JSON.parse(`curl #{ENDPOINT.gsub("{{phrase_id}}", phrase_id.to_s)} --silent`).inject({}) do |ret, (locale, trans)|
@camertron
camertron / composition_exclusions.rb
Created July 28, 2012 22:29
Dumping composition exclusions into yml
require 'open-uri'
require 'fileutils'
require 'yaml'
PROPS_URL = "http://www.unicode.org/Public/6.1.0/ucd/DerivedNormalizationProps.txt"
OUTPUT_FILE = "/tmp/cldr/composition_exclusions.yml"
EXPECTED_TOTAL_POINTS = 1120
data = open(PROPS_URL).read
start_pos = data.index("# Derived Property: Full_Composition_Exclusion")
@camertron
camertron / decompositon_map.rb
Created July 28, 2012 23:17
Dumping the decomposition map to yml
require 'twitter_cldr'
require 'fileutils'
require 'yaml'
OUTPUT_FILE = "/tmp/cldr/decomposition_map.yml"
CODE_POINT_MAX = 1114111
decomps = {}
CODE_POINT_MAX.times do |i|
code_point = TwitterCldr::Shared::CodePoint.for_hex(i.to_s(16))
@camertron
camertron / count_ones.rb
Last active October 13, 2015 18:17
Count 1 bits in integer (Ruby)
# protip: use Ruby 1.9 for block support with Enumerable#count
def count_ones(num)
return 0 if num == 0
digits = Math.log2(num).floor + 1
0.upto(digits).count do |i|
num & (2 ** i) == (2 ** i)
end
end
@camertron
camertron / mountain-grade.txt
Created February 25, 2013 04:19
Slope of Mountain
/
/ |
/ |
116,435 / |
/ | 8,000
/ x |
___________
116,160
soh cah toa
@camertron
camertron / activerecord_outer_join.rb
Last active December 14, 2015 16:49
Outer Join on an Association in ActiveRecord
class Project < ActiveRecord::Base
has_and_belongs_to_many :phrases
end
class Phrase < ActiveRecord::Base
has_and_belongs_to_many :projects
end
join_dependency = ActiveRecord::Associations::JoinDependency.new(Project, [:phrases], [])
@camertron
camertron / index.html
Last active December 20, 2015 15:19
CSKit Annotations from Strong's Concordance
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>CSKit HTML Example</title>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<style>
.cskit-strongs-word {
@camertron
camertron / immutable_strings.rb
Last active December 21, 2015 19:49
Making Ruby Strings Immutable by Default
module BeforeMethodHook
def before(*names)
names.each do |name|
m = instance_method(name)
define_method(name) do |*args, &block|
yield self
m.bind(self).call(*args, &block)
end
end
end