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 / weasel.rb
Last active August 29, 2015 14:02 — forked from jlindsey/weasel.rb
class Weazel
CHARS = [*'A'..'Z', ' ']
def initialize target
@target = target
@best = Array.new(28) { CHARS.sample }.join
end
def call
0.upto Float::INFINITY do |n|
class Integer
def collatz_chain_size
n = self
chain = []
until n == 1
chain << n
n.even? ? n /= 2 : n = n * 3 + 1
end
[chain.size, chain.first]
end
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 / 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
require 'benchmark'
hash = {}
Benchmark.measure do
1_000_000.times do
hash = {}
end
end.real
#=> 0.245904
@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 / 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

Ruby Number Conversion Chart

From To Expression
array = (1..12).map &:to_s
class Array
def insert_every n, this
each_slice(n).inject { |r, a| r << this << a }.flatten
end
end
array.insert_every 3, ','
#=> ["1", "2", "3", ",", "4", "5", "6", ",", "7", "8", "9", ",", "10", "11", "12"]
require "./alfred"
results = Alfred::Feedback.new
available_outputs = `./SwitchAudioSource -a | grep "(output)" | sed 's/ (output)$//'`
current_output = `./SwitchAudioSource -c`
def ditch_line available, current
available.split("\n").delete_if { |line| line == current }.join("\n")
end