- showterm - http://www.showterm.io
- izzy - http://www.github.com/baweaver
- pry-vterm-alias - https://github.com/envygeeks/pry-vterm_aliases
- pry-remote - https://github.com/mon-ouie/pry-remote
- pry-remote-em - https://github.com/simulacre/pry-remote-em
- pry-rescue - https://github.com/ConradIrwin/pry-rescue
- pry-macro - https://github.com/baweaver/pry-macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prompt(name: '', matches: '') | |
while true | |
print "#{name}: "; answer = gets.chomp | |
return answer if Array(matches).all? { |match| match === answer } | |
puts "Invalid!" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Kernel | |
def defn name, &block | |
define_method name, block | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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(:+) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer