Skip to content

Instantly share code, notes, and snippets.

View seejohnrun's full-sized avatar
🤗
Writing codes

John Crepezzi seejohnrun

🤗
Writing codes
View GitHub Profile
@seejohnrun
seejohnrun / example.rb
Last active August 29, 2015 14:05
satirical ruby
class String
def |(other)
other.call(*self)
end
def >(other)
File.write(other, *self)
0
end
end
@seejohnrun
seejohnrun / tap_try.rb
Last active August 29, 2015 14:05
tap_try should exist
# Mark NoMethodError(s) coming from a call on `nil`
class NilClass
def method_missing(*)
super
rescue NoMethodError => e
e.instance_variable_set :@real_nil, true
raise e
end
end
@seejohnrun
seejohnrun / map_count.rb
Created November 7, 2014 18:38
Do want
module Enumerable
def map_count
Hash.new(0).tap { |h| each { |e| h[yield e] += 1 } }
end
end
puts [1, 2, 3, 4, 5].map_count(&:odd?) # {true=>3, false=>2}
# split into groups of maximum size BATCH_SIZE
groups = []; grp = nil
items.each_with_index do |item, idx|
groups << (grp = []) if idx % BATCH_SIZE == 0
grp << item
end
@seejohnrun
seejohnrun / Coming soon
Created February 9, 2011 02:52
Twitter Streaming Search Growl Notifications
#!/usr/bin/env ruby
require 'rubygems'
require 'twitterstream'
module MassStream
CREDS = [
{:username => 'xxx', :password => 'xxx'},
]
@seejohnrun
seejohnrun / gist:824374
Created February 13, 2011 02:44
Local short urls to avoid a short_urls table
class ShortId
# We cut out vowels to avoid shortened strings from mistakenly
# forming words
Alphabet = 'bcdfghjklmnpqrstvwxyz0123456789BCDFGHJKLMNPQRSTVWXYZ'
AlphabetLength = Alphabet.length
# Encode a numeric ID
def self.encode(id)
alpha = ''
@seejohnrun
seejohnrun / extra_indices.rb
Created February 24, 2011 16:49
Detect unnecessary indices in your database
require 'rubygems'
require 'active_record'
# Look through each table and look for indexes that are subsets
# of each other.
ActiveRecord::Base.establish_connection(:database => 'elfcast_development', :adapter => 'mysql')
ActiveRecord::Base.connection.tables.each do |table|
# go through each index
indexes = ActiveRecord::Base.connection.indexes(table)
@seejohnrun
seejohnrun / gist:3188573
Created July 27, 2012 15:10
MessagePack & JSON
require 'yajl'
require 'rest-client'
require 'zlib'
require 'msgpack'
require 'benchmark'
require 'colorize'
def compressed(d)
output = StringIO.new
gz = Zlib::GzipWriter.new(output)
@seejohnrun
seejohnrun / settings.js
Created November 13, 2012 21:12
reverting defaults
var settings = Object.create({
_defaults: {},
_values: {},
setDefault: function (key, value) {
this._values[key] = this[key]
this._defaults[key] = value
Object.defineProperty(this, key, {
get: function () {
var undef = typeof this._values[key] === 'undefined'
return undef ? this._defaults[key] : this._values[key]
@seejohnrun
seejohnrun / at_once.rb
Created November 14, 2012 00:29
Enumeration functions that run async (would people want to see this in a library?)
module AtOnce
extend self
def map(objects, &block)
results = Array.new(objects.count)
threads = objects.map.with_index do |object, idx|
Thread.new do
results[idx] = block.call(object)
end