Skip to content

Instantly share code, notes, and snippets.

Avatar

Oleg Andreev oleganza

View GitHub Profile
View gist:239365
def smallAsciiRepresentation(digest)
digestParts = digest.unpack("Q2") # digest is a 128 bit buffer, cut it into two 64 bits ruby Integer
smallDigestPart = digestParts[0] ^ digestParts[1] # XOR the two 64 bits parts
smallDigest = [smallDigestPart].pack("Q") # and transform the result into a 64 bits binary buffer
smallDigestB64 = [smallDigest].pack("m") # b64-encode the result
smallID = smallDigestB64.gsub(/[\n=]/, '').gsub(/\+/,'-').gsub(/\//,'_') # and make it URL-friendly by shortening it (no '=' at the end, no '+', no '/')
return smallID
end
View Tests Is Wrong
In response to all the responses to:
http://twitter.com/rtomayko/status/1155906157
You should never do this in a source file included with your library,
app, or tests:
require 'rubygems'
The system I use to manage my $LOAD_PATH is not your library/app/tests
View gist:30701
# Given a set of documents this method returns a list of tags associated with
# those documents ordered by the ones occuring on the most documents. Tags th
# only appear on one doc are excluded from the list.
# If the user supplies a specific tag to exclude it will not be included
# in the list.
def related_tags(docs, exclude_tag = nil)
# DM triggers single SQL query for all docs' tags when doc.tags is called.
docs[0,20].inject({}) do |hash, doc|
doc.tags.inject(hash) do |hsh, tag|
hsh[tag] ||= 0
View oleganza-suggestion.rb
# Sometimes :symbol.operator looks nice.
# I generally hate "global variables programming",
# but this extension seems to be safe and handy.
def experimental_by_oleganza
# arrays for OR, hashes for AND with in-Symbol operators
all([{:name => ["john", "jane"]}, {:age.gr => 3, :age.lt => 65}])
# same as above, but with optional :any, :all keys (:any for OR, :all for AND)
all([{:name => ["john", "jane"]}, {:any => [{:age.gt => 3}, {:parental_control.not => :nil}], :age.lt => 65}])
View gist:25529
Generates a byte code for the "if" expression.
To understand how it works consider the
following Scheme code:
(if (> 3 8) 10 14)
The expression results in the following byte code:
array('i', [9, 0, 9, 1, 5, 6, 1, 2, 17, 14, 9, 2, 16, 16, 9, 3])