From 1a9738ff11f02fad328923a3254d725be184d362 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 3 Jun 2011 03:22:59 +1200 Subject: [PATCH] refactored example initialization code (still need to update examples for new API) --- examples/example_basic.rb | 6 ++---- examples/example_command_override.rb | 7 ++----- examples/example_commands.rb | 5 +---- examples/example_hooks.rb | 7 ++----- examples/example_image_edit.rb | 12 ++++-------- examples/example_input.rb | 5 +---- examples/example_input2.rb | 5 +---- examples/example_output.rb | 5 +---- examples/example_print.rb | 7 ++----- examples/example_prompt.rb | 7 ++----- examples/helper.rb | 6 ++++++ 11 files changed, 24 insertions(+), 48 deletions(-) create mode 100644 examples/helper.rb diff --git a/examples/example_basic.rb b/examples/example_basic.rb index 8f893be6..ed3d4d52 100644 --- a/examples/example_basic.rb +++ b/examples/example_basic.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # define a local. a = "a local variable" @@ -14,4 +11,5 @@ end # Start pry session at top-level. # The local variable `a` and the `hello` method will # be accessible. +puts __LINE__ binding.pry diff --git a/examples/example_command_override.rb b/examples/example_command_override.rb index 889eaeee..237b1d61 100644 --- a/examples/example_command_override.rb +++ b/examples/example_command_override.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # Inherit standard command set, but tweak them by importing some and # overriding others. @@ -18,7 +15,7 @@ class MyCommands < Pry::CommandBase # analogy to Ruby's native alias_method idiom for decorating a method alias_command "old_status", "status", "" - + # Invoke one command from within another using `run` command "status", "Modified status." do |x| output.puts "About to show status, are you ready?" diff --git a/examples/example_commands.rb b/examples/example_commands.rb index ad57e021..8e945db1 100644 --- a/examples/example_commands.rb +++ b/examples/example_commands.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) class MathCommands < Pry::CommandBase command "greet", "Greet a person, e.g: greet john" do |name| diff --git a/examples/example_hooks.rb b/examples/example_hooks.rb index 132b1ea1..e2a66b70 100644 --- a/examples/example_hooks.rb +++ b/examples/example_hooks.rb @@ -1,12 +1,9 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) my_hooks = { :before_session => proc { |out, target| out.puts "Opening #{target.eval('self')}." }, :after_session => proc { |out, target| out.puts "Closing #{target.eval('self')}." } } - + # Start a Pry session using the hooks hash defined in my_hooks Pry.start(TOPLEVEL_BINDING, :hooks => my_hooks) diff --git a/examples/example_image_edit.rb b/examples/example_image_edit.rb index 1eefa5e4..5f200c85 100644 --- a/examples/example_image_edit.rb +++ b/examples/example_image_edit.rb @@ -1,7 +1,7 @@ # Note: this requires you to have Gosu and TexPlay installed. # `gem install gosu` # `gem install texplay` -# +# # Extra instructions for installing Gosu on Linux can be found here: # http://code.google.com/p/gosu/wiki/GettingStartedOnLinux # @@ -10,11 +10,7 @@ # # Have fun! :) -direc = File.dirname(__FILE__) - -require 'rubygems' -require "texplay" -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) WIDTH = 640 HEIGHT = 480 @@ -42,12 +38,12 @@ class WinClass < Gosu::Window @img.rect 0, 0, @img.width - 1, @img.height - 1 @binding = Pry.binding_for(@img) - + @pry_instance = Pry.new(:commands => ImageCommands, :prompt => IMAGE_PROMPT) end def draw - @img.draw_rot(WIDTH / 2, HEIGHT / 2, 1, 0, 0.5, 0.5) + @img.draw_rot(WIDTH / 2, HEIGHT / 2, 1, 0, 0.5, 0.5) end def update diff --git a/examples/example_input.rb b/examples/example_input.rb index dd7c8d4e..dfa2239a 100644 --- a/examples/example_input.rb +++ b/examples/example_input.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # Create a StringIO that contains the input data str_input = StringIO.new("puts 'hello world!'\nputs \"I am in \#{self}\"\nexit") diff --git a/examples/example_input2.rb b/examples/example_input2.rb index bd4979d5..13a9364b 100644 --- a/examples/example_input2.rb +++ b/examples/example_input2.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # Create a StringIO that contains the input data for all the Pry objects cmds = <<-CMDS diff --git a/examples/example_output.rb b/examples/example_output.rb index e54dad77..6711a7b0 100644 --- a/examples/example_output.rb +++ b/examples/example_output.rb @@ -1,7 +1,4 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # Create a StringIO to contain the output data str_output = StringIO.new diff --git a/examples/example_print.rb b/examples/example_print.rb index 0b55a31a..330982ea 100644 --- a/examples/example_print.rb +++ b/examples/example_print.rb @@ -1,9 +1,6 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) my_print = proc { |out, value| out.puts "Output is: #{value.inspect}" } - + # Start a Pry session using the print object defined in my_print Pry.start(TOPLEVEL_BINDING, :print => my_print) diff --git a/examples/example_prompt.rb b/examples/example_prompt.rb index 44b68092..f11c5ef7 100644 --- a/examples/example_prompt.rb +++ b/examples/example_prompt.rb @@ -1,12 +1,9 @@ -direc = File.dirname(__FILE__) - -require 'rubygems' -require "#{direc}/../lib/pry" +require File.expand_path(File.join(File.dirname(__FILE__), 'helper')) # Remember, first prompt in array is the main prompt, second is the wait # prompt (used for multiline input when more input is required) my_prompt = [ proc { |obj, *| "inside #{obj}> " }, proc { |obj, *| "inside #{obj}* "} ] - + # Start a Pry session using the prompt defined in my_prompt Pry.start(TOPLEVEL_BINDING, :prompt => my_prompt) diff --git a/examples/helper.rb b/examples/helper.rb new file mode 100644 index 00000000..ce37f493 --- /dev/null +++ b/examples/helper.rb @@ -0,0 +1,6 @@ +require 'rubygems' +unless Object.const_defined? 'Pry' + $:.unshift File.expand_path '../../lib', __FILE__ + require 'pry' +end +