Skip to content

Instantly share code, notes, and snippets.

📝
Documenting things

Brandon Weaver baweaver

📝
Documenting things
Block or report user

Report or block baweaver

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@baweaver
baweaver / retrieve_keys
Created Feb 5, 2014
retrieve all keys listed
View retrieve_keys
[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 Feb 14, 2014
repeatedly prompt until something matches all conditions
View prompt.rb
def prompt(name: '', matches: '')
while true
print "#{name}: "; answer = gets.chomp
return answer if Array(matches).all? { |match| match === answer }
puts "Invalid!"
end
end
View gist:9315733
# 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 Mar 23, 2014
Because I can
View defn.rb
module Kernel
def defn name, &block
define_method name, block
end
end
View primeperms.rb
# 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 Aug 29, 2015
java vs scala - which is easier to read?
View gist:11240360
// 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 / prime_perms2.rb
Last active Aug 29, 2015
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.
View prime_perms2.rb
# 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 / factor_conditionals.rb
Created Jun 26, 2014
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.
View factor_conditionals.rb
[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 / static_typed.rb
Last active Aug 29, 2015
Ala banisterfiend's cleverness: https://gist.github.com/banister/acec9b4688cbb7575106 - I made it do something else interesting
View static_typed.rb
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
You can’t perform that action at this time.