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 / 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
# Patch numbers to have a prime? method
class Fixnum
def prime?
([1,2].include?(self) || !(2..Math.sqrt(self).ceil).find { |x| self % x == 0 })
end
end
# Basic implementation - 42 minutes
(1..1_000_000).select { |n|
n.to_s
@baweaver
baweaver / gist:11240360
Last active August 29, 2015 14:00
java vs scala - which is easier to read?
// Compute Fibonacci numbers, ex: 1 1 2 3 5 8 13 21...
// A fib number is the result of adding the previous two,
// such that fib(3) is the result of fib(2) + fib(1)
// Scala
def fib(i:Int):Int = i match{
case 0 => 0
case 1 => 1
case _ => fib(i-1) + fib(i-2)
}
@baweaver
baweaver / factor_conditionals.rb
Created June 26, 2014 04:01
Only mildly confounded, and the difference is only truly pronounced at larger orders. Given a complicated enough conditional in a block though and this could have a serious effect.
[24] pry(main)> def test(range, gt=nil)
[24] pry(main)* range.select { |n| gt ? n > gt : n }.reduce(:+)
[24] pry(main)* end
=> nil
[25] pry(main)> def test2(range, gt=nil)
[25] pry(main)* filter = gt ? -> n { n > gt } : -> n { n }
[25] pry(main)* range.select(&filter).reduce(:+)
@baweaver
baweaver / prime_perms2.rb
Last active August 29, 2015 14:03
Variant of Prime Perms, checking mersennes from 1 to 1 million, using block extraction for time comparisons. Surprising results were found when t3 dominated the field.
# From original: https://gist.github.com/baweaver/11163861
# Using Pipeable for clearer code: https://github.com/baweaver/pipeable
# and Benchpress to measure: https://github.com/baweaver/benchpress
# Make Object Pipeable
class Object; include Pipeable end
# Patch numbers to have a prime? method
class Fixnum
def prime?
@baweaver
baweaver / static_typed.rb
Last active August 29, 2015 14:04
Ala banisterfiend's cleverness: https://gist.github.com/banister/acec9b4688cbb7575106 - I made it do something else interesting
def static_typed(types={})
type, method_name = types.first
old_method = instance_method(method_name)
define_method(method_name) do |*args, &block|
old_method.bind(self).call(*args, &block).tap { |return_value|
raise TypeError, "Return value is not of type #{type}!" unless type === return_value
}
end
end