Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
View GitHub Profile
@havenwood
havenwood / gist:5605341
Last active December 17, 2015 11:49 — forked from anonymous/gist:5605311
def fib n
a, b = 1, 2
sum = 0
while a < n
sum += a if a.even?
a, b = b, a + b
end
sum
end
require 'benchmark'
hash = {}
Benchmark.measure do
1_000_000.times do
hash = {}
end
end.real
#=> 0.245904
@havenwood
havenwood / foo.rb
Last active December 25, 2015 23:39
str = "aaa [first_name] bar [first_name] [last_name]"
last = str.scan(/\[last_name\]/).size
first = str.scan(/\[first_name\]/).size
remaining = str.gsub(/\[(first_name|last_name)\]/, '').size
first * 15 + last * 10 + remaining
class MyClass
attr_accessor :user, :type
def initialize user: 'smith', type: 'pleb'
@user = user
@type = type
end
end
both = MyClass.new user: 'blah', type: 'pleb'
just_one = MyClass.new type: 'pleb'
@havenwood
havenwood / learn.rb
Last active March 16, 2016 11:15 — forked from tylerneylon/learn.lua
Learn Ruby: A fork of Learn Lua (https://gist.github.com/tylerneylon/5853042)
# An octothorp (#) starts a single-line comment.
=begin
Starting with a =being and ending with a =end
makes it a multi-line comment.
=end
##
# 1. Variables and flow control.
#
@havenwood
havenwood / cpus.rb
Created June 23, 2016 20:54 — forked from parshap/cpus.rb
Determine number of cpu cores to use in Vagrant
# Determines how many cpus the virtual machine should be given. Uses half of
# what's available on the host with a default of 4.
def numvcpus
begin
os_cpu_cores / 2
rescue
4
end
end
@havenwood
havenwood / installfest.md
Last active October 21, 2016 20:05 — forked from rubydiamond/installfest.md
Requirements to get a sticker and be ready for the workshop day.

Rails Girls L.A. The Third Level

[ ] I have an editor. It is ___________________.


[ ] I can show you where my terminal is and a few commands.

  • I can show where I am in the directories.
  • List the files in a particular directory.
  • Make a directory.
require 'fiddle'
class Object
def cast(as)
# Make sure iv table of the new class gets initialized.
as.new.send(:instance_variable_set, :@nil, nil)
rbasic = Fiddle::Pointer.new(__id__ << 1)
as_klass = Fiddle::Pointer.new(as.__id__ << 1)
# Ruby guarantees sizeof(VALUE) == sizeof(void *)
# Identity functor that does not give any additional structure
Identity = Struct.new(:value) do
def fmap(func = nil, &blk)
f = func || blk;
Identity.new(f.call(self.value))
end
end
# Const Functor that preserves its content
Const = Struct.new(:value) do
@havenwood
havenwood / bench
Last active June 6, 2018 06:02
benchmark for json, yajl and oj.
~ ruby json_benchmark.rb
String to JSON
Warming up --------------------------------------
Oj.dump 24.918k i/100ms
Yajl.dump 9.461k i/100ms
JSON.dump 24.797k i/100ms
MessagePack.dump 44.355k i/100ms
Calculating -------------------------------------
Oj.dump 267.683k (± 3.4%) i/s - 1.346M in 5.032971s
Yajl.dump 98.929k (± 2.8%) i/s - 501.433k in 5.072850s