Skip to content

Instantly share code, notes, and snippets.

View norman's full-sized avatar
🪕
Scruggs not drugs

Norman Clarke norman

🪕
Scruggs not drugs
View GitHub Profile

Various snippets that were annoying to create and that I don't want to forget

vim regexp to convert CSS to SASS:

g/^(\s*)([a-z-]):\s*(.*);/s//\1:\2 \3/g

module MyNamespace
class << self
# I want this utility method available everywhere inside my namespace,
# but I don't want to expose it in the API, so let's make it "protected."
def do_something
puts "hello"
end
protected :do_something
end
# Here's a piece of functionality I need to access often inside my library. It breaks an int
# into an array of bits, with each bit's position in the int corresponding to its array
# index. This gives me a consise way to check to see if lots of different bits are set.
123.to_s(2).reverse.split("").map(&:to_i)
# So, where do I put this? Should I monkey-patch Fixnum?
class Fixnum
def to_bit_array
to_s(2).reverse.split("").map(&:to_i)
end
$ lua -e 'for i=1,4 do print(string.byte("ザ", i)) end'
227
130
182
$ lua -e 'print(string.char(227, 130, 182))'
# It's pretty easy to set a custom method as the basis of your friendly_id if
# you want to incorporate information from another model in the friendly_id.
class City < ActiveRecord::Base
belongs_to :state
has_friendly_id :city_and_state, :use_slugs => true
# This will return something like "San Francisco, California". It can be used
# as a display name in your views, and also as the basis of the friendly_id
# because it will be processed by the plugin to remove spaces, punctuation,
# $ mysql --version
# mysql Ver 14.14 Distrib 5.1.43, for apple-darwin10.2.0 (i386) using EditLine wrapper
# $ psql --version
# psql (PostgreSQL) 8.4.2
# contains support for command-line editing
# $ sqlite3 --version
# 3.6.12
# $ ruby --version
# ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.2.0]
# $ uname -a
Selecting 10 rows from a table with 100,000 records:
MYSQL | SQLITE3 | POSTGRES |
--------------------------------------------------------
Ordered SELECT id 0.021 | 0.040 | 0.021 |
Ordered SELECT * 0.001 | 0.001 | 0.006 |
Random SELECT id 0.070 | 0.132 | 0.040 |
Random SELECT * 0.607 | 0.176 | 0.061 |
MYSQL | SQLITE3 | POSTGRES |
--------------------------------------------------------
Ordered SELECT id 0.042 | 0.060 | 0.062 |
Ordered SELECT * 0.034 | 0.031 | 0.032 |
Random SELECT id 7.072 | 12.960 | 3.303 |
Random SELECT * 61.000 | 17.350 | 4.904 |

Some random examples of Spanish words transcribed to International Phonetic Alphabet by my in-progress spanish linguistics library and phonology library.

If you're interested in this stuff, you may want to take a look at how it first transcribes orthography to a base IPA form, then applies phonological rules to the resulting sequence. Then, syllabification and stress rules are applied using the Sequence of Sonority princple.

I'm currently working on making the phonological rules configurable so that dialectical phenomena like [yeísmo](http://en.wikipedia.org/wiki/Ye%C

rvm_scripts_path=/Users/norman/.rvm/scripts
rvm_bin_path=/Users/norman/.rvm/bin
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/bash
TERM=screen
HISTSIZE=10000
TMPDIR=/var/folders/0Z/0ZnTrwlWGnWgSuCxSuEox++++TI/-Tmp-/
Apple_PubSub_Socket_Render=/tmp/launch-DMDMjb/Render
TERM_PROGRAM_VERSION=273
USER=norman