Skip to content

Instantly share code, notes, and snippets.

View baweaver's full-sized avatar
📝
Documenting things

Brandon Weaver baweaver

📝
Documenting things
View GitHub Profile
@baweaver
baweaver / rails.yml
Created October 22, 2013 23:00
A Blueprint for installing Rails. Demonstration for upcoming Handyman gem.
# Blueprint for Installing Rails
# ==============================
#
# Makes Ruby 2 and Rails 4
Ruby:
RVM:
- 'apt-get install curl'
- 'curl -L https://get.rvm.io | bash'
- 'rvm install 2.0'
@baweaver
baweaver / pk-install
Created November 3, 2013 00:32
I get really annoyed at having to retype this exact same thing to install every tar.gz package, so I put it in a function
function pk-install {
url="$1"
wget $url
file_name=`echo $url | sed 's/^.*\/\(.*\)\.tar\.gz$/\1/'`
tar -zxvf "$file_name.tar.gz"
cd $file_name
make && make install
}
@baweaver
baweaver / LatLong Regex
Created December 22, 2013 01:31
Ruby regex for Latitude and Longitude
/\([+-]?((?=9)90(\.0+)?|[1-8]?\d(\.\d+)?), [+-]?((?=1\d{2})((?=18\d)180(\.0+)?|1[0-7]\d(\.\d+)?)|\d{2}(\.\d+)?)\)/
@baweaver
baweaver / string-symbol
Created January 3, 2014 05:04
String vs Symbol instantiation. When possible, prefer symbols, as they have on average half the instantiation time.
[9] pry(main)> n = 1_000
=> 1000
[10] pry(main)> Benchmark.bm { |bm|
[10] pry(main)* bm.report { n.times { :foo }}
[10] pry(main)* bm.report { n.times { 'foo'}}
[10] pry(main)* }
user system total real
0.000000 0.000000 0.000000 ( 0.000192)
0.000000 0.000000 0.000000 ( 0.000396)
=> [#<Benchmark::Tms:0x0000000263dfd8
@baweaver
baweaver / gist:8450274
Created January 16, 2014 05:30
Testing library: Proof. Vague musings on structure.
def my_function(n); n / 2; end
prove x.will eq(5).when { x = my_function(10) }
when { x = my_function(10) }.prove_that_it_will eq(5)
my_context = ->{ x = my_function(10) }
prove(x).will eq(5).when my_context
@baweaver
baweaver / Skyforge
Last active January 3, 2016 13:09
A working draft for Skyforge, a super powered mocking framework.
# Skyforge
Forge mock objects for your test with immense power! This one is currently in development,
and not close to being done. So what are we planning to do here?
## !!! THIS IS THEORETICAL, UNDER CONSTRUCTION !!!
## Trace
Using Mockingjay, trace a ruby hash to make a generator for more objects like it.
@baweaver
baweaver / retrieve_keys
Created February 5, 2014 01:49
retrieve all keys listed
[1] pry(main)> hash = {a: 1, b: 2, c: 3}
=> {:a=>1, :b=>2, :c=>3}
[2] pry(main)> class Hash
[2] pry(main)* def retrieve_keys(*keys)
[2] pry(main)* self.reduce({}) { |h, (k, v)|
[2] pry(main)* keys.include?(k) ? h.merge!({k => v}) : h
[2] pry(main)* }
[2] pry(main)* end
[2] pry(main)* end
=> nil
@baweaver
baweaver / prompt.rb
Created February 14, 2014 03:27
repeatedly prompt until something matches all conditions
def prompt(name: '', matches: '')
while true
print "#{name}: "; answer = gets.chomp
return answer if Array(matches).all? { |match| match === answer }
puts "Invalid!"
end
end
@baweaver
baweaver / gist:9315733
Last active August 29, 2015 13:56
validates
# Playing off of my prompt idea, let's take it one step further:
name = prompt for: 'Name', validate: { with: /\w+/, or: 'Invalid name given!' }
# Where if given invalid input it will say:
# Error: Invalid name given
# Name:
@baweaver
baweaver / defn.rb
Created March 23, 2014 20:50
Because I can
module Kernel
def defn name, &block
define_method name, block
end
end