Skip to content

Instantly share code, notes, and snippets.

@toothrot
toothrot / woot.md
Created April 22, 2011 18:24
syntax test
D, Y = 8, 0
8===D-- ( Y )
@toothrot
toothrot / strings.rb
Created April 22, 2011 00:09
Single vs Double quotes
require 'benchmark'
Benchmark.bmbm do |b|
b.report("single") do
10_000.times do
'some string'
end
end
b.report("double") do
10_000.times do
"some string"
/* yes - output a string repeatedly until killed
Copyright (C) 1991-1997, 1999-2004, 2007-2011 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
irb(main):034:0> D, Y = 8, 0
=> [8, 0]
irb(main):035:0> 8===D-- ( Y )
=> true
@toothrot
toothrot / 1.rb
Created January 19, 2011 22:30
underscoring json in ruby
def self.underscore_hash(hash)
hash.inject({}) do |underscored, (key, value)|
value = underscore_hash(value) if value.is_a?(Hash)
value = value.map { |v| underscore_hash(v) } if value.is_a?(Array)
underscored[key.underscore] = value
underscored
end
end
module PostFile
def post_file(url, file_path, field_name = 'file')
file_name = File.basename(file_path)
boundary = "AaB03x"
data = <<EOF
--#{boundary}\r
Content-Disposition: form-data; name="#{field_name}"; filename="#{file_name}"\r
\r
#{File.read(file_path)}\r
--#{boundary}--\r
@toothrot
toothrot / does_sort_preserve_order.rb
Created July 22, 2010 17:20
does_sort_preserve_order
ary = [
{:sort_me => 1, :expected_order => 1},
{:sort_me => 2, :expected_order => 7},
{:sort_me => 2, :expected_order => 8},
{:sort_me => 1, :expected_order => 2},
{:sort_me => 1, :expected_order => 3},
{:sort_me => 1, :expected_order => 4},
{:sort_me => 1, :expected_order => 5},
{:sort_me => 1, :expected_order => 6},
].sort! { |a,b| a[:sort_me] <=> b[:sort_me] }
@toothrot
toothrot / ruby_shuffle.c
Created July 21, 2010 03:19
shuffling arrays for 1.8.6
static VALUE
rb_ary_shuffle_bang(ary)
VALUE ary;
{
long i = RARRAY(ary)->len;
rb_ary_modify(ary);
while (i) {
long j = rb_genrand_real()*i;
VALUE tmp = RARRAY(ary)->ptr[--i];
@toothrot
toothrot / riot_example.rb
Created July 20, 2010 21:19 — forked from gus/riot_example.rb
dick jokes
require 'rubygems'
require 'riot'
context "Something" do
setup do
D = 8
end
asserts("big") { 8==D }
asserts("bigger") { 8===D }
require 'rubygems'
require 'riot'
context "Something" do
setup do
D = 8
end
asserts("something") { 8==D }
asserts("something else") { 8===D }