Skip to content

Instantly share code, notes, and snippets.

View Burgestrand's full-sized avatar
🙃
(:

Kim Burgestrand Burgestrand

🙃
(:
View GitHub Profile
@Burgestrand
Burgestrand / assets.rake
Created October 6, 2010 10:02
Asset packaging rake task
# Rake task for compiling CoffeeScript and SASS (Compass).
#
# Author: Kim Burgestrand <http://burgestrand.se/>
# Date: 6th October 2010
# License: X11 License (MIT License)
# URL: http://gist.github.com/gists/613114
desc "Compiles CoffeeScript using Barrista (but only if they changed)"
task 'coffee:compile' => :environment do
require 'barista'
abort "'#{Barista::Compiler.bin_path}' is unavailable." unless Barista::Compiler.available?
@Burgestrand
Burgestrand / em-fiber.rb
Created March 6, 2011 18:54
An experiment using Fibers together with EventMachine
# coding: utf-8
require 'eventmachine'
module EM
module Strand
extend self
# Fires up EventMachine in a fiber, ready to take orders.
#
# @return EM::Strand
class Proc
def merge_binding!(other_binding)
other_binding.eval("local_variables").each do |var|
var_id = other_binding.eval(var.to_s).object_id
binding.eval("#{var} = ObjectSpace._id2ref(#{var_id})")
end
end
end
x = 3
@unders
unders / ruby-self-and-default-definee.rb
Created September 14, 2011 07:21
self is the self which you know. It is the default receiver of method invocation. There is always a self.
class Object
def singleton_class
class << self
self
end
end
end
# X.singleton_class
# - > X
@Burgestrand
Burgestrand / hallon_enumerator.rb
Created September 26, 2011 21:40
Proof of concept for a lazy Enumerator
require 'ostruct'
require 'delegate'
module Hallon
class Enumerator < SimpleDelegator
attr_reader :size
def initialize(size, pointer, &block)
@pointer = pointer
@items = Array(0...size)
@Burgestrand
Burgestrand / replacer.rb
Created December 8, 2011 16:58
Using s/string/string/ to replace things in strings… YAY RUBY!
class Proc
alias :/ :call
end
def s
optmap = {
"x" => Regexp::EXTENDED,
"i" => Regexp::IGNORECASE,
"m" => Regexp::MULTILINE
}
@Burgestrand
Burgestrand / eval_bang.rb
Created February 14, 2012 20:22
A Ruby method that evals a string of code and returns a hash of all locals defined inside
# Eval a string of code, returning a hash of all local variables and their values.
#
# @param [String] code
# @param [Binding] binding
# @param [String] file (shows up in backtrace if an error occurs)
# @param [Integer] line (shows up in backtrace if an error occurs)
# @return [Hash] a map of :local_var => value_of_that_var
def eval!(__code__, binding = nil, file = __FILE__, line = (__LINE__ + 1))
eval <<-SOURCE, binding, file, line
#{__code__}
@ryandotsmith
ryandotsmith / instruments.rb
Created April 6, 2012 07:09
Sequel & Sinatra Instrumentation
module Instruments
def self.set_logger(l, m)
@logger = l
@method = m
end
def self.logger
@logger
end
@Burgestrand
Burgestrand / whitespace.rb
Created August 30, 2012 07:13
A whitespace thingy in Ruby, just for the lulz
# coding: utf-8
class Whitespace
def initialize(&block)
@data = 0
@pos = -1
instance_eval(&block)
end
def  
tap { @pos += 1 }
@Raynos
Raynos / weak-map.js
Last active September 18, 2019 07:49 — forked from Gozala/weak-map.js
Harmony WeakMap shim for ES5
// Original - @Gozola. This is a reimplemented version (with a few bug fixes).
window.WeakMap = window.WeakMap || (function () {
var privates = Name()
return {
get: function (key, fallback) {
var store = privates(key)
return store.hasOwnProperty("value") ?
store.value : fallback
},