Hi Folks - I am running some ruby tests and would like to print out
the actual test to the console or store it to a file. The tests are
written as classes with a setup, run, and teardown method.
So here's my question: is there a way to get the class definition or
method definitions as a "string", so that I can print it out (to a
file or console)?
I know ruby supports a lot of reflection (like .methods
or .constants), but I'm not seeing how to do this.
Thanks!
Hi Folks - I am running some ruby tests and would like to print out
the actual test to the console or store it to a file. The tests are
written as classes with a setup, run, and teardown method.
So here's my question: is there a way to get the class definition or
method definitions as a "string", so that I can print it out (to a
file or console)?
I know ruby supports a lot of reflection (like .methods
or .constants), but I'm not seeing how to do this.
A very non-elegant, trivial way using Ruby's DATA stream :
# test.rb
def foo
puts "hi"
end
flag = nil
DATA.tap{|x| x.rewind}.readlines.each do |line|
flag = true if line =~ /^def/
puts line if flag
flag = false if line =~ /^end/
end
__END__
$ ruby test.rb
def foo
puts "hi"
end
Obviously you will have to come up with better heuristics to determine
the end of a method.
···
--
Anurag Priyam
http://about.me/yeban/
Adam Lauper wrote in post #982302:
Hi Folks - I am running some ruby tests and would like to print out
the actual test to the console or store it to a file. The tests are
written as classes with a setup, run, and teardown method.
So here's my question: is there a way to get the class definition or
method definitions as a "string", so that I can print it out (to a
file or console)?
I know ruby supports a lot of reflection (like .methods
or .constants), but I'm not seeing how to do this.
Thanks!
I just released a gem which gives access to the source code of methods
and procs (http://github.com/quix/live_ast\). See the "to_ruby" section
of the readme. Since the string returned by to_ruby is a parsed/unparsed
version of your code, it probably won't match the original source (and
comments will be gone), but it may suffice for your needs.
···
--
Posted via http://www.ruby-forum.com/\.
In Ruby 1.9, you can retrieve the place where the method was defined using #source_location:
def foo
end
method(:foo).source_location #=> ["file.rb", 1]
The same goes for procs:
def foo(&block)
puts block.source_location.inspect
end
foo do
end #=> ["file.rb", 5]
Regards,
Florian
···
On Feb 17, 2011, at 8:40 PM, Anurag Priyam wrote:
Hi Folks - I am running some ruby tests and would like to print out
the actual test to the console or store it to a file. The tests are
written as classes with a setup, run, and teardown method.
So here's my question: is there a way to get the class definition or
method definitions as a "string", so that I can print it out (to a
file or console)?
I know ruby supports a lot of reflection (like .methods
or .constants), but I'm not seeing how to do this.A very non-elegant, trivial way using Ruby's DATA stream
:
# test.rb
def foo
puts "hi"
endflag = nil
DATA.tap{|x| x.rewind}.readlines.each do |line|
flag = true if line =~ /^def/
puts line if flag
flag = false if line =~ /^end/
end
__END__$ ruby test.rb
def foo
puts "hi"
endObviously you will have to come up with better heuristics to determine
the end of a method.
I haven't looked at James's gem, but if live_ast can map AST
structures to lines in the source, then it'd be easy to determine
which range of lines in a file is the interesting part. Then you'd
know precisely what section to grab.
~ jf
···
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: User John Feminella - Stack Overflow
On Fri, Feb 18, 2011 at 09:30, James M. Lawrence <quixoticsycophant@gmail.com> wrote:
Adam Lauper wrote in post #982302:
Hi Folks - I am running some ruby tests and would like to print out
the actual test to the console or store it to a file. The tests are
written as classes with a setup, run, and teardown method.
So here's my question: is there a way to get the class definition or
method definitions as a "string", so that I can print it out (to a
file or console)?
I know ruby supports a lot of reflection (like .methods
or .constants), but I'm not seeing how to do this.
Thanks!I just released a gem which gives access to the source code of methods
and procs (http://github.com/quix/live_ast\). See the "to_ruby" section
of the readme. Since the string returned by to_ruby is a parsed/unparsed
version of your code, it probably won't match the original source (and
comments will be gone), but it may suffice for your needs.--
Posted via http://www.ruby-forum.com/\.
Hi Folks - I am running some ruby tests and would like to print out
the actual test to the console or store it to a file. The tests are
written as classes with a setup, run, and teardown method.
So here's my question: is there a way to get the class definition or
method definitions as a "string", so that I can print it out (to a
file or console)?
I know ruby supports a lot of reflection (like .methods
or .constants), but I'm not seeing how to do this.A very non-elegant, trivial way using Ruby's DATA stream
:
# test.rb
def foo
puts "hi"
endflag = nil
DATA.tap{|x| x.rewind}.readlines.each do |line|
flag = true if line =~ /^def/
puts line if flag
flag = false if line =~ /^end/
end
__END__
Cool, I didn't realize you access the source file using tap and rewind.
$ ruby test.rb
def foo
puts "hi"
endObviously you will have to come up with better heuristics to determine
the end of a method.In Ruby 1.9, you can retrieve the place where the method was defined using #source_location:
def foo
end
method(:foo).source_location #=> ["file.rb", 1]
The same goes for procs:
def foo(&block)
puts block.source_location.inspect
endfoo do
end #=> ["file.rb", 5]
I just discovered #source_location a week or so ago, but I immediately
wondered if there was a way to discern the end of a block or method. I
never found one. Might one be added in a future Ruby?
···
On Thu, Feb 17, 2011 at 4:53 PM, Florian Gilcher <flo@andersground.net> wrote:
On Feb 17, 2011, at 8:40 PM, Anurag Priyam wrote: