Skip to content

Instantly share code, notes, and snippets.

View jrunning's full-sized avatar

Jordan Running jrunning

View GitHub Profile
@jrunning
jrunning / 01_trim_quotes_benchmark.rb
Last active February 20, 2016 18:19
trim_quotes.rb
require 'benchmark/ips'
require 'securerandom'
def trim_quotes_wiktor(str)
str.gsub(/\A"(.*)"\Z/m, '\1')
end
def trim_quotes_sawa(str)
str.dup.tap do |s|
s[0] = s[-1] = "" if s[0] == '"' and s[-1] == '"'
require 'benchmark/ips'
def data
Array.new(40000) do
[ rand(-100..0), rand(0..100) ]
end
end
def assign_destruct(data)
data.map {|a| a[0] = a[0].abs.to_s; a }
@jrunning
jrunning / rescue-vs-respond-to.rb
Last active December 10, 2015 23:30
rescue-vs-respond-to.rb
require 'benchmark/ips'
module RescueVsRespondTo
module HelperClasses
class Receiver
def dummy; true end
def does_exist; true end
end
class Great20Grandparent < Receiver; end
@jrunning
jrunning / base58.rb
Last active November 17, 2020 03:55
UUID to Base 58 in Ruby
# base58_to_int and int_to_base58 loosely based on base58 gem by Douglas F. Shearer
# https://github.com/dougal/base58
ALPHABET = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ".chars
BASE = ALPHABET.size
def base58_to_int(base58_val)
base58_val.chars
.reverse_each.with_index
.reduce(0) do |int_val, (char, index)|
require "benchmark/ips"
class Test
def initialize(test_value)
@test_value = test_value
assert_equal(self_merge_with_block(@test_value), map_then_hash(@test_value))
end
def self_merge_with_block(hsh)
@jrunning
jrunning / columns_equal.rb
Last active August 29, 2015 14:10
Benchmark checking columnwise equality in two-dimensional arrays
require "benchmark/ips"
ARRAY_SIZE = 5 # 5 rows and 5 columns
CHANCE = 0.33
# Returns the given 5-element array with a 33% chance of one item being changed
def dirty_row(row)
return row if rand > CHANCE
row.dup.tap { row[rand 0..(ARRAY_SIZE - 1)] = rand 0..99 }
end
### Keybase proof
I hereby claim:
* I am jrunning on github.
* I am jrunning (https://keybase.io/jrunning) on keybase.
* I have a public key whose fingerprint is 19E4 9FBC E975 8DC8 B15F A5D3 2004 7142 D7E6 D44A
To claim this, I am signing this object:
@jrunning
jrunning / ruby_encoding_gotcha.rb
Created November 12, 2014 21:00
Ruby encoding w/ string interpolation gotcha
str = "ascii".force_encoding(Encoding::ASCII_8BIT)
puts "#{str}foo".encoding # => ASCII-8BIT
puts " #{str}foo".encoding # => UTF-8

Okay, let's work through this iteratively, starting with your example:

str = "2:README:19:string:Hello world!spec.rb:20:string:describe RBFS1:rbfs:4:0:0:"
entries = {} # No entries yet!

The very first thing we need to know is how many files there are, and we know we know that's the number before the first ::

num_entries, rest = str.split(':', 2)
num_entries = Integer(num_entries)

num_entries is now 2

@jrunning
jrunning / fun_with_to_proc.rb
Last active August 29, 2015 14:07
Fun with to_proc
class Regexp
def to_proc
proc {|str| match(str) && $~.to_s }
end
end
arr = [ "foo", "bar 123", "baz", "456", "789 qux" ]
p arr.find(&/bar/) # => "bar 123"