Skip to content

Instantly share code, notes, and snippets.

Avatar

James Tucker raggi

View GitHub Profile
View dispatch_test.rb
require "benchmark"
@callbacks = %w(
TimerFired
ConnectionData
ConnectionUnbound
ConnectionAccepted
ConnectionCompleted
LoopbreakSignalled
ConnectionNotifyReadable
@raggi
raggi / benches.rb
Created Jul 16, 2009
bigdecimal char_round
View benches.rb
require "benchmark"
require 'bigdecimal'
class BigDecimal
# raggi after hint from apeiros (round 1)
def raggi_round(n = 15)
s = to_s('f')[0,n+1]
i = s.index('.')
dp = n - i
dp = 0 if dp < 0
@raggi
raggi / scope_challenge.rb
Created Jul 29, 2009
Should this work? Do you know what it's doing?
View scope_challenge.rb
# The Question: Should this work?
# The Bonus Question: Does it, and in what capacity?
def parse_message(str)
last = nil
@parser ||= Parser.new { |message| last = message }
@parser << str
last
end
View array_concat_vs_push.rb
require 'benchmark'
def many
10000
end
def push(left_array_size, right_array_size)
left = Array.new(left_array_size) { |n| n }
right = Array.new(right_array_size) { |n| n }
Benchmark.realtime{ many.times{ |i| left.push(*right) } }
View bench_frozen_strings.rb
require "benchmark"
STRING = '1234567890'
FROZEN = STRING.dup.freeze
FROZEN_KEY = {
FROZEN => :something
}
View bench_constant_vs_literal_hash_keys.rb
require "benchmark"
KEY = '1234567890'
HASH = {
KEY => KEY
}
TESTS = 2_000_000
View example.rb
def times_for_period(date)
regular_hours_by_week = Hash.new(0)
regular_hours = 0
other_hours = Hash.new(0)
work_days_for_period(date).each do |d|
times = d.times
regular_hours += times[:regular_hours]
@raggi
raggi / quicktalk.rb
Created Nov 12, 2009
A quick hack to post messages to talker chat rooms
View quicktalk.rb
#!/usr/bin/env ruby
domain, room, *message = ARGV
token = File.read(ENV['HOME'] + '/.talker').strip
abort <<-TEXT unless domain && room && token
Usage: #{File.basename($0)} domain room_id [message]
Reads from stdin if no message supplied
Expects a user token in $HOME/.talker
TEXT
@raggi
raggi / alias_task.rake
Created Nov 12, 2009
A helper to alias a task in rake
View alias_task.rake
def alias_task(name, old_name)
t = Rake::Task[old_name]
desc t.full_comment if t.full_comment
task name, *t.arg_names do |_, args|
# values_at is broken on Rake::TaskArguments
args = t.arg_names.map { |a| args[a] }
t.invoke(args)
end
end
@raggi
raggi / sync_io_bug.rb
Created Dec 2, 2009
ruby sync io bug on fbsd + osx, 1.8
View sync_io_bug.rb
# This bug seems to occur on 1.8.6..1.9 on OSX and FreeBSD. It's known not to occur on Linux.
require 'rubygems'
require 'minitest/unit'
include MiniTest::Assertions
require 'tempfile'
path = nil
begin