Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Works in irb #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/method_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "#{direc}/method_source/version"
require "#{direc}/method_source/source_location"
require "#{direc}/method_source/code_helpers"
require "#{direc}/method_source/irb"

module MethodSource
extend MethodSource::CodeHelpers
Expand Down Expand Up @@ -49,6 +50,7 @@ def self.comment_helper(source_location, name=nil)
# @return [Array<String>] the contents of the file
# @raise [SourceNotFoundError]
def self.lines_for(file_name, name=nil)
return IRB.CurrentContext.source if file_name == "(irb)"
@lines_for_file ||= {}
@lines_for_file[file_name] ||= File.readlines(file_name)
rescue Errno::ENOENT => e
Expand Down
24 changes: 24 additions & 0 deletions lib/method_source/irb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# From the gem "cute_print", and before that from the gem "wrong"

if defined? IRB
module IRB
class Context

def source
@all_lines.join
end

original_evaluate = instance_method(:evaluate)

# Save every line that is evaluated. This gives a way to get
# the source when it is run in irb.
define_method :evaluate do |src, line_no|
@all_lines ||= []
@all_lines += ["\n"] * (line_no - @all_lines.size - 1)
@all_lines += src.lines.to_a
original_evaluate.bind(self).call src, line_no
end

end
end
end
10 changes: 10 additions & 0 deletions method_source.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ Gem::Specification.new do |s|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<bacon>, ["~> 1.1.0"])
s.add_development_dependency(%q<rake>, ["~> 0.9"])
# Under some versions of Rubinius, when the tests are run with
# `bundle exec rake`, the irb tests failes with:
# rubysl-irb is not part of the bundle. Add it to Gemfile.
# This seems to be a bug in some version of Rubinius:
# https://gitter.im/rubinius/rubinius/archives/2015/04/14
# Since this is only a development dependency, we can get away
# with the the condition.
if Object.const_defined?("RUBY_ENGINE") && RUBY_ENGINE == 'rbx'
s.add_development_dependency(%q<rubysl-irb>)
end
else
s.add_dependency(%q<bacon>, ["~> 1.1.0"])
s.add_dependency(%q<rake>, ["~> 0.9"])
Expand Down
18 changes: 18 additions & 0 deletions test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'rubygems'
require 'bacon'
require 'shellwords'
require "#{direc}/../lib/method_source"
require "#{direc}/test_helper"

Expand Down Expand Up @@ -135,4 +136,21 @@ class << $o; self; end.instance_method(:hello).source.should == @hello_singleton
method(:comment_test5).comment.should == @comment5
end
end

it "should work in irb" do
lib_path = File.join(File.dirname(__FILE__), "../lib")
command = [
"irb",
"-I", lib_path,
].shelljoin
output = IO.popen(command, "r+") do |io|
io.puts 'require "method_source"'
io.puts 'l = lambda { }'
io.puts 'puts "l = #{l.source.inspect}"'
io.close_write
io.read
end
output.should.include 'l = "l = lambda { }\n"'
end

end