Skip to content

Instantly share code, notes, and snippets.

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 / 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
irb(main):034:0> D, Y = 8, 0
=> [8, 0]
irb(main):035:0> 8===D-- ( Y )
=> true
/* 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
@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"
@toothrot
toothrot / woot.md
Created April 22, 2011 18:24
syntax test
D, Y = 8, 0
8===D-- ( Y )
@toothrot
toothrot / benchmark.rb
Created May 17, 2011 20:55
set vs array
require 'benchmark'
require 'set'
some_numbers = []
1_000_000.times do
num = rand(100_000)
rand(5).times do
some_numbers << {:id => num}
end
@toothrot
toothrot / 1-mentalguy.rb
Created May 17, 2011 21:43
i don't like tap
# from http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions
# For debugging and getting a puts in there, tap is nice
def blah
@things.map { |x|
x.length
}.inject( 0 ) { |a, b|
a + b
}
end
@toothrot
toothrot / powerset.rb
Created May 18, 2011 22:20
powerset in ruby
class Array
def powerset
inject([[]]){ |c,y|
c.inject([]) {|r,i|
r+=[i,i+[y]]
}
}
end
end
# = delegate -- Support for the Delegation Pattern
#
# Documentation by James Edward Gray II and Gavin Sinclair
#
# == Introduction
#
# This library provides three different ways to delegate method calls to an
# object. The easiest to use is SimpleDelegator. Pass an object to the
# constructor and all methods supported by the object will be delegated. This
# object can be changed later.