Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
$count = 0
def out *args
# During resizing events, terminals may not do a carriage return
# on their own, so we have to explicitly insert it
print "#{args.join ' '}\r\n"
end
command = lambda { |x|
@acook
acook / gist:4424574
Created January 1, 2013 01:31
Quick comparison of YAML, JSON, and BSON. Results were.. inconclusive. O_o
1.9.3> require 'bson'
=> true
1.9.3> require 'json'
=> true
1.9.3> require 'yaml'
=> true
1.9.3> h = {foo: 'bar'}
=> {:foo=>"bar"}
1.9.3> b = BSON.serialize h
=> foobar
@acook
acook / log_reader.rb
Last active December 11, 2015 06:39
Log reader can tail logs and will restart logs that have been rotated. Subclass and replace the `process` method!
class LogReader
def self.open filename
new File.open filename, 'r'
end
def initialize file
raise ArgumentError, 'File required!' unless File === file
@file = file
end
attr :file
@acook
acook / wtf.rb
Last active December 11, 2015 18:28
Some serious weirdness with Ruby hashes.
files = Hash.new Array.new
file = Pathname.new '../.DS_Store'
hash = Digest::SHA256.file file
# This results in weirdness
files[hash.to_s] << file
files #=> {}
files.length #=> 0
file[hash.to_s] #=> [#<Pathname:../.DS_Store>]
## Single pass
$ time bin/rivalry ..
-- Size : 299 MB
-- Count : 1456 files
-- Dupes : 301 files
real 0m2.604s
user 0m2.435s
sys 0m0.163s
@acook
acook / config_module.rb
Created February 6, 2013 23:59
Extensible configuration objects from YAML
module ConfigModule
def [] key
config.send key
end
def config
@config ||= load_config
end
def config_file file
@acook
acook / irb.rb
Created February 7, 2013 00:29 — forked from bkudria/irb.rb
LazyStruct.new({:a=>1, :b => 2}).a # => 2
# That's wrong. Why?
@acook
acook / math_hack.rb
Last active December 14, 2015 05:08
Attempt to implement the "math hack" described here: http://9gag.com/gag/6663551 WiP, this is just a quick kludge and doesn't work 100% yet.
#99 x 99 = 9312 |
def reset
"\e[0m"
end
def mult l, r
lb = 10 ** l.to_s.size
rb = 10 ** r.to_s.size
@acook
acook / 0_Desired_Specs.txt
Last active December 14, 2015 05:29
Laptop comparison
Ideal Specs
CPU: 3.7Ghz QuadCore 3rd Gen i7
Mem: 16GB @ 1600MHz
GPU: NVIDIA GeForce GTX 680MX
HDD: 750GB SSD
Wifi: 802.11N
Screen: 13"/14" 1600x900
Bluetooth: Yes
@acook
acook / pig_latin.rb
Created March 22, 2013 19:02
PigLatin Translator!
#!/usr/bin/env ruby
# Translates a text file to Pig Latin!
class PigLatin
def initialize filename = nil
@filename = filename
end
attr :filename
def translate!