Skip to content

Instantly share code, notes, and snippets.

View swarley's full-sized avatar

Matt Carey swarley

View GitHub Profile
@swarley
swarley / gist:4109319
Created November 19, 2012 06:56
pry-hack/load
[2] pry(main)> require "pry/hack/load"
=> true
[3] pry(main)> Pry.add_hack(:method, :peek_ivar, Pry::Hackage::Hack.new(/@(.+?)$/) { replace_with "instance_variable_get(:@#{capture 1})" })
=> #<Pry::Hackage::Hack:0x00000002540838
@CODE=#<Proc:0x000000025407e8@(pry):3>,
@PATTERN=/@(.+?)$/>
[4] pry(main)> Pry.config.hack.enabled = true
=> true
[5] pry(main)> class Test; def initialize; @ivar = :defined; end; end
=> nil
@swarley
swarley / hook.rb
Created November 19, 2012 20:47
pry/pry-hack
class Pry
if VERSION <= "0.9.10"
alias old_retrieve_line retrieve_line
def retrieve_line(eval_string, *args)
old_retrieve_line(eval_string, *args)
eval_string.sub!(/\A.+?\Z/, Hackage.hack_line(eval_string))
end
else
hooks.add_hook(:foo, :pry_hack) do {|line| Hackage.hack_line(line) }
swarley@OctaviasViolin ~/Programming/pry $ rake test
bacon -Ispec -rubygems -a -q spec/syntax_checking_spec.rb spec/commands/ls_spec.rb spec/commands/find_method_spec.rb spec/commands/cat_spec.rb spec/completion_spec.rb spec/command_spec.rb spec/commands/help_spec.rb spec/history_array_spec.rb spec/input_stack_spec.rb spec/commands/jump_to_spec.rb spec/sticky_locals_spec.rb spec/commands/whereami_spec.rb spec/commands/amend_line_spec.rb spec/indent_spec.rb spec/exception_whitelist_spec.rb spec/commands/show_source_spec.rb spec/method_spec.rb spec/commands/bang_spec.rb spec/pry_defaults_spec.rb spec/commands/save_file_spec.rb spec/code_spec.rb spec/command_integration_spec.rb spec/commands/raise_up_spec.rb spec/command_set_spec.rb spec/commands/edit_method_spec.rb spec/commands/edit_spec.rb spec/prompt_spec.rb spec/commands/show_doc_spec.rb spec/pry_output_spec.rb spec/control_d_handler_spec.rb spec/wrapped_module_spec.rb spec/commands/hist_spec.rb spec/command_helpers_spec.rb spec/commands/exit_all_spec.rb sp
[5] pry(main)> show-source
From: /usr/local/lib/ruby/1.9.1/psych/core_ext.rb @ line 1:
Number of lines: 18
class Object
def self.yaml_tag url
Psych.add_tag(url, self)
end
@swarley
swarley / gist:4186786
Created December 2, 2012 03:13
What are your thoughts.
# This method is to hook sequences of Tokens from the lexer and create a new hack-hook (unintended alliteration)
# @example
# on_sequence RubyToken::TkNLINE, [RubyToken::TkSPACE], '*', RubyToken::TkNLINE do
# ... # TODO: Add in example code for this, I havn't decided on an API for Hackage::Sequence yet,
# end
# The purpose of these sequences are to detect whether we have a matching possibility for a hack, which would determine
# the necesity or lack there of to parse hooked objects in the hack type, the given example would be an implementation for
# a hack that would allow the hooking of hacks that targeted specifically single line operations.
#
# WARNING: This API is subject to many changes as I examine the way that Hackage::Lexer deals with its Tokens and whether or not they are left as
rxp = /((?<=[^'"])(?<=.)?\|(?!.*(?!do\s*(\|.*\|)*|\{(\|.*\|)*|\'|\")))/
=> /((?<=[^'"])(?<=.)?\|(?!.*(?!do\s*(\|.*\|)*|\{(\|.*\|)*|\'|\")))/
r = RxpBuilder.build { (!any_of("'",'"').in_the_past & (!`.`).in_the_past.maybe) << `\\|` << !`.`.repeat.maybe << !`do` << `\\s`.repeat.maybe << (`\\|.*\\|`).maybe | `\\{` << `\\|.*\\|`.maybe | `'` | `"` }
=> /(?:(?:(?:(?=[^'"])(?=.*(?:(?!.))?)\|(?!(?:(?:.)+)?)(?!do)(?:\s)*(?:\|.*\|)?)|(?:\{(?:\|.*\|)?))|(?:'))|(?:")/
# Complicated :/
def indent(input)
@lexer.set_input StringIO.new(input)
output = ""
last_seek = 0
seek = 0
while token = @lexer.token
# Check for newline
if token.is_a? RubyToken::TkNL
seek = token.seek
# Take all characters that were from the previous line and add them
# Test
case foo
when 1
2
when 2
if 3
4
end
when 5
#
[0] (walnut)> def test
[0] (walnut)* print "Hello "
[0] (walnut)* ensure
[0] (walnut)* puts "world!"
[0] (walnut)* end
puts "foo"
### report: Hello <%= "Test" %> world!
puts "bar"
def test_a_method
foo_bar = rand(160)
### Report: The random number recieved in <%= __method__ %> is <%= foo_bar %>
end