diff options
author | John Mair <jrmair@gmail.com> | 2010-12-17 04:34:57 +1300 |
---|---|---|
committer | John Mair <jrmair@gmail.com> | 2010-12-17 04:34:57 +1300 |
commit | 9c0afdcf3f57e224b33f587804c307aa891a0d7f (patch) | |
tree | baa61f8ea92427d9546f0f91594ac2c08fa4fba9 | |
parent | 2895bb245fd216be96da6e4d6b9e361248934450 (diff) | |
download | method_source-9c0afdcf3f57e224b33f587804c307aa891a0d7f.tar.gz |
added warning about YARV only (due to RubyVM, so not JRuby compatible). Also added support and tests for Proc#source
-rw-r--r-- | README.markdown | 8 | ||||
-rw-r--r-- | lib/method_source.rb | 4 | ||||
-rw-r--r-- | lib/method_source/version.rb | 2 | ||||
-rw-r--r-- | test/test.rb | 50 |
4 files changed, 45 insertions, 19 deletions
diff --git a/README.markdown b/README.markdown index 0d73f38..7a389f9 100644 --- a/README.markdown +++ b/README.markdown @@ -6,7 +6,7 @@ method_source _retrieve the sourcecode for a method_ `method_source` is a utility to return a method's sourcecode as a -Ruby string. +Ruby string. Also returns `Proc` and `Lambda` sourcecode. It is written in pure Ruby (no C). @@ -17,8 +17,8 @@ It is written in pure Ruby (no C). * Read the [documentation](http://rdoc.info/github/banister/method_source/master/file/README.markdown) * See the [source code](http://github.com/banister/method_source) -example: ---------- +Example: methods +---------------- Set.instance_method(:merge).source.display # => @@ -35,7 +35,7 @@ example: Limitations: ------------ -* Only works with Ruby 1.9+ +* Only works with Ruby 1.9+ (YARV) * Cannot return source for C methods. * Cannot return source for dynamically defined methods. diff --git a/lib/method_source.rb b/lib/method_source.rb index bdb00ed..59c98f3 100644 --- a/lib/method_source.rb +++ b/lib/method_source.rb @@ -92,3 +92,7 @@ end class UnboundMethod include MethodSource::MethodExtensions end + +class Proc + include MethodSource::MethodExtensions +end diff --git a/lib/method_source/version.rb b/lib/method_source/version.rb index 30c0683..1f52d87 100644 --- a/lib/method_source/version.rb +++ b/lib/method_source/version.rb @@ -1,3 +1,3 @@ module MethodSource - VERSION = "0.1.0" + VERSION = "0.1.1" end diff --git a/test/test.rb b/test/test.rb index 8a4beaa..05a711d 100644 --- a/test/test.rb +++ b/test/test.rb @@ -4,29 +4,51 @@ require 'bacon' require "#{direc}/../lib/method_source" hello_source = "def hello; :hello; end\n" +lambda_source = "MyLambda = lambda { :lambda }\n" +proc_source = "MyProc = Proc.new { :proc }\n" def hello; :hello; end -describe MethodSource do +MyLambda = lambda { :lambda } +MyProc = Proc.new { :proc } - it 'should define methods on both Method and UnboundMethod' do +describe MethodSource do + it 'should define methods on Method and UnboundMethod and Proc' do Method.method_defined?(:source).should == true UnboundMethod.method_defined?(:source).should == true + Proc.method_defined?(:source).should == true end - - if RUBY_VERSION =~ /1.9/ - it 'should return source for method' do - method(:hello).source.should == hello_source - end - it 'should raise for C methods' do - lambda { method(:puts).source }.should.raise RuntimeError - end + describe "Methods" do + if RUBY_VERSION =~ /1.9/ + it 'should return source for method' do + method(:hello).source.should == hello_source + end - else - it 'should raise on #source' do - lambda { method(:hello).source }.should.raise RuntimeError + it 'should raise for C methods' do + lambda { method(:puts).source }.should.raise RuntimeError + end + + else + it 'should raise on #source' do + lambda { method(:hello).source }.should.raise RuntimeError + end end end -end + describe "Lambdas and Procs" do + if RUBY_VERSION =~ /1.9/ + it 'should return source for proc' do + MyProc.source.should == proc_source + end + + it 'should return source for lambda' do + MyLambda.source.should == lambda_source + end + else + it 'should raise on #source' do + lambda { method(:hello).source }.should.raise RuntimeError + end + end + end +end |