Skip to content

Instantly share code, notes, and snippets.

Avatar
©️
 ​[object Object] :trollface:

Aaron Patterson tenderlove

©️
 ​[object Object] :trollface:
View GitHub Profile
View rake_in_30_lines.rb
require "set"
module RakeTask
CALLED = Set.new
NAMESPACES = []
TASKS = Hash.new { |h, k| h[k] = Struct.new(:blocks, :deps).new([], []) }
class << self
def call(name)
return if CALLED.include?(name)
View gist:9702
class CommentsTest < NodeTestCase
def test_line_numbers
parser = RKelly::Parser.new
ast = parser.parse(<<-eojs)
/**
* This is an awesome test comment.
*/
function aaron() { // This is a side comment
var x = 10;
return 1 + 1; // America!
View meta.rb
require "rubygems"
require "johnson"
require "./visitor.rb"
js =<<-END
function top() {
"top docstring", { command: true }
doSomething();
};
View gist:14187
require 'nokogiri'
require 'rubygems'
require 'builder'
require 'benchmark'
n = 50_000
Benchmark.bm(7) do |x|
x.report("builder: ") {
n.times {
builder = Builder::XmlMarkup.new(:indent => 1)
View gist:14188
require 'nokogiri'
require 'rubygems'
require 'builder'
require 'benchmark'
require 'faster_builder/xml_markup'
n = 50_000
Benchmark.bm(7) do |x|
x.report("builder: ") {
n.times {
View gist:14196
require 'nokogiri'
require 'rubygems'
require 'benchmark'
require 'faster_builder/xml_markup'
n = 50_000
Benchmark.bm(7) do |x|
x.report("nokogiri: ") {
n.times {
Nokogiri::XML::Builder.new {
View gist:14218
####
# Get the dimensions for a Jpeg
#
class JpegFile
def initialize file
@io = File.open(file, 'rb')
end
def dimensions
raise 'Not a jpeg' unless @io.read(2).unpack('CC') == [0xFF, 0xD8]
View gist:15180
####
# ( ++ B ).to_i #=> 3
# ( ++- B ).to_i #=> 6
# ( ++-+ B ).to_i #=> 13
# ( 0 + ++-+ B ) #=> 13
class B
class << self
def +@; new.send(:"+@"); end
def -@; new.send(:"-@"); end
end
View gist:16819
module Color
class RGB < Struct.new(:r, :g, :b)
def to_hsl
(r,g,b) = *([self.r, self.g, self.b].map { |x| x / 255.0 })
min = [r,g,b].min
max = [r,g,b].max
l = (max + min) / 2.0
return HSL.new(0, 0, (l * 100).round) if max == min
View gist:17305
####
# Morse code for ruby
#
# puts ( +-~+-~+-+~---~-+ M ) # => 'aaron'
#
class M
DITS_N_DAHS = {
'+-' => 'a',
'-+++' => 'b',
'-+-+' => 'c',