Skip to content

Instantly share code, notes, and snippets.

View swarley's full-sized avatar

Matt Carey swarley

View GitHub Profile
[63] pry(main)> Pokemon.battle(Pokemon::PIKACHU, Pokemon::PIKACHU)
The moves avalible for pokemon #1 are:
Thundershock | Growl | Tail Whip | Thunder Wave
1
The moves avalible for pokemon #2 are:
Thundershock | Growl | Tail Whip | Thunder Wave
3
Pikachu is attacking Pikachu for 20 HP with Thundershock
Pikachu is attacking Pikachu for 0 HP with Tail_Whip
=> -25
Pry::Commands.create_command "find" do
description "Recursively search a Module/Class for a method : find [REGEX] [MODULE/CLASS]"
def process
regex = eval "/#{args[0..-2].join(' ')}/"
base = ::Class.const_get args.last
ret = search(regex, base)
output.puts ret.flatten
end
Pry::Commands.create_command "find-method" do
description "find-method: Find a method recursively USAGE: find-method [OPTION] [REGEX] [BASECLASS]"
def options(opti)
#method_options(opti)
opti.on :n, :name, "Search for a method by name"
opti.on :c, :content, "Search for a method based on content in Regex form"
end
[111] pry(main)> class Tester
[111] pry(main)* def test_method
[111] pry(main)* geewhiz = 1
[111] pry(main)* end
[111] pry(main)* end
=> nil
[112] pry(main)> find-method -c 'gee.+' Tester
Tester#test_method
Pry::Commands.create_command "find-method" do
description "find-method"
def options(opti)
opti.on :n, :name, "Search for a method by name"
opti.on :c, :content, "Search for a method based on content in Regex form"
end
def process
Pry::Commands.create_command "find-method" do
description "find-method"
def options(opti)
opti.on :n, :name, "Search for a method by name"
opti.on :c, :content, "Search for a method based on content in Regex form"
end
def process
def dist(size, n)
m = size % n
d = (n-m) % size
([d]*(n-m)) + ([1]*(m))
end
p dist(10, 12)
# [2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
def dist(size, n)
m = size % n
d = (n-m) % size
([d]*(n-m)) + ([1]*(m))
end
p dist(10, 12)
# [2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
@swarley
swarley / gist:4107917
Created November 18, 2012 22:39
pry/pry-hack
[1] pry(main)> load "./hack.rb"
/usr/local/lib/ruby/gems/1.9.1/gems/reg-0.4.8/regdeferred.rb:53: warning: undefining `object_id' may cause serious problems
/usr/local/lib/ruby/gems/1.9.1/gems/reg-0.4.8/regdeferred.rb:53: warning: undefining `object_id' may cause serious problems
/usr/local/lib/ruby/gems/1.9.1/gems/reg-0.4.8/regdeferred.rb:53: warning: undefining `object_id' may cause serious problems
/usr/local/lib/ruby/gems/1.9.1/gems/reg-0.4.8/regdeferred.rb:53: warning: undefining `object_id' may cause serious problems
/usr/local/lib/ruby/gems/1.9.1/gems/reg-0.4.8/regdeferred.rb:53: warning: undefining `object_id' may cause serious problems
^[[A=> true
[2] pry(main)> Pry.add_hack(:method, Pry::Hackage::Hack.new(/^\@([A-Za-z0-9_]+)$/) { replace_with "instance_variable_get(:@#{capture 1})" })
=> 28680540
[3] pry(main)> class Test; def initialize; @o = 1; end; end
@swarley
swarley / pry-hack_modulo.rb
Created November 19, 2012 00:55
pry/pry-hack
[1] pry(main)> load "./hack.rb"; Pry.config.hack.enabled = true
=> true
[2] pry(main)> Pry.add_hack(:%, :symbol_array, Pry::Hackage::ModOpHack.new('S') { replace_with "%w#{capture 1}#{capture 2}#{capture 3}.map(&:to_sym)" })
=> #<Pry::Hackage::ModOpHack:0x00000001081140
@CHAR="S",
@CODE=#<Proc:0x00000001088f80@(pry):2>>
[3] pry(main)> %S{hello symbol world! i bet everyone is jealous of pry now}
=> [:hello, :symbol, :world!, :i, :bet, :everyone, :is, :jealous, :of, :p