This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AssignValueOnlyOnceHash < Hash | |
KeyReassignmentError=Class.new(RuntimeError) | |
def []=(key, val) | |
raise KeyReassignmentError, "key '#{key}' already assigned" if key? key | |
super | |
end | |
end | |
if __FILE__==$PROGRAM_NAME | |
require 'test/unit' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> require 'active_support/all' | |
=> true | |
>> hwia = HashWithIndifferentAccess[:a, 1, :b, 2] | |
=> {:a=>1, :b=>2} # uh... ok so maybe they didn't define self.[]= . but all is not lost... yet. let's continue. | |
>> hwia | |
=> {:a=>1, :b=>2} # well, they're supposed to always be strings internally, but let's try anyway | |
>> hwia['a'] | |
=> nil # ORLY. | |
>> hwia.merge!({:a => 11, 'a' => 12}) | |
=> {:a=>1, :b=>2, "c"=>3, "a"=>12} # For fuck's sake. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HashList < Hash | |
def add(k) | |
self[k] = true | |
end | |
alias include? key? | |
end | |
module ResourceConsumptionProfiler | |
extend self | |
def profile_resource_consumption(params = {}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Observe: | |
class MyAwesomeToDos | |
ALLOWED_THINGS = [:beer, :whiskey, :women, :clean_code] #.freeze | |
def initialize(params) | |
@params = params | |
end | |
def allowed_things |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ostruct' | |
module HashToOpenstruct | |
def to_ostruct | |
o = OpenStruct.new(self) | |
each.with_object(o) do |(k,v), o| | |
o.send(:"#{k}=", v.to_ostruct) if v.respond_to? :to_ostruct | |
end | |
o | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Encryption functions. Requires the GNUpg "gpg" commandline tool. On OS X, "brew install gnupg" | |
# Explanation of options here: | |
# --symmetric - Don't public-key encrypt, just symmetrically encrypt in-place with a passphrase. | |
# -z 9 - Compression level | |
# --require-secmem - Require use of secured memory for operations. Bails otherwise. | |
# cipher-algo, s2k-cipher-algo - The algorithm used for the secret key | |
# digest-algo - The algorithm used to mangle the secret key | |
# s2k-mode 3 - Enables multiple rounds of mangling to thwart brute-force attacks | |
# s2k-count 65000000 - Mangles the passphrase this number of times. Takes over a second on modern hardware. | |
# compress-algo BZIP2- Uses a high quality compression algorithm before encryption. BZIP2 is good but not compatible with PGP proper, FYI. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby environment tweaking | |
export RUBY_HEAP_MIN_SLOTS=1000000 | |
export RUBY_HEAP_SLOTS_INCREMENT=1000000 | |
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1 | |
export RUBY_GC_MALLOC_LIMIT=100000000 | |
export RUBY_HEAP_FREE_MIN=500000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ...I got 99 problems, but after this, fork and thread safety won't be one... | |
module Process | |
alias fork_without_freeze fork | |
def fork_with_freeze(&block) | |
result = fork_without_freeze(&block) | |
ObjectSpace.each_object(Class){|c| c.freeze } | |
result | |
end | |
alias fork fork_with_freeze |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'digest/sha2' | |
shared_secret = "yabbadabbadoo" | |
provider_salt = "peter" | |
seconds_since_epoch = Time.now.to_i | |
minutes_since_epoch = seconds_since_epoch / 60 | |
cleartext = shared_secret + provider_salt + minutes_since_epoch.to_s | |
hash = Digest::SHA2.new << cleartext |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/sha1' | |
Dir.glob("**/api/v2/**/*.rb").inject(0){|acc,f| acc ^ Digest::SHA1.hexdigest(File.read(f)).to_i(16) } |