[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

rubyinline

Trans

8/25/2006 10:57:00 PM

Hi--

I managed to generalize my 'rubytest' this afternoon into a nice
general purpose comment code block runner. It defaults to unit tests,
but you can use if for any variety of embedded code such as examples.
I'm sure this can be improved upon. For instance the current version
doesn't deal with #-style comments, but only =begin...=end blocks. Let
me know if you have any other suggestions.

I do have one pressing issue though. I came up with two ways to run the
code. 1) using eval, and 2) using a pipe. The first is better b/c I can
maintain the line numbers, so that error reports point to the right
place. OTOH the second way allows all the normal command line options
that ruby handles to be passed along. I would like to achieve both of
these rather than one or the other. Both methdos are presented in the
code below. Any ideas on how to fix?

Hope you all like and find this useful. It will be included in the next
release of my build tools.

Thanks,
T.


# rubyinline

#! /usr/bin/ruby1.8

class InlineRunner

# This runs the commented code block directly.
# This has an advantage in that the line numbers
# can be maintained.

def run_eval( fname, block='test' )
code, offset = extract_block( fname )

require 'test/unit' if block == 'test'
require fname

eval code, TOPLEVEL_BINDING, File.basename(fname), offset
end

# This runs the commented code block via a pipe.
# This has an advantage in that all the parameters
# that can be passed to ruby can be passed to rubyinline.

def run_pipe( fname, block='test' )
code, offset = extract_block( fname, block )

code = "require 'test/unit'\n\n" + code if block == 'test'
code = "require '#{fname}'\n\n" + code

cmd = ['ruby', *ARGV].join(' ')

result = IO.popen(cmd,"w+") do |ruby|
ruby.puts code
ruby.close_write
puts ruby.read
end
end

# Show rubyinline help.

def help
helpstr = `ruby --help`
helpstr.sub!('ruby', 'rubyinline')
puts helpstr
end

private

#

def pattern( block )
b = Regexp.escape( block )
/^=begin\s+#{b}.*?\n(.*)\n=end/mi
end

#

def extract_block( fname, block='test' )
code = File.read( fname )
md = pattern( block ).match( code )
code = md ? md[1] : nil
unless code
puts "Code block not found -- #{block}"
exit 0 #return nil
end
offset = code.split(/\n/).size - code.split(/\n/).size - 1
return code, offset
end

end

if $0 == __FILE__

irun = InlineRunner.new

if ARGV.delete('--help')
irun.help
exit 0
end

if i = ARGV.index('-b')
block = ARGV[i+1].strip
ARGV[i+1,1] = nil
ARGV.delete('-b')
else
block = 'test'
end

file = ARGV.pop
irun.run_eval(file, block)
#irun.run_pipe(file, block)

end

19 Answers

pat eyler

8/25/2006 11:10:00 PM

0

On 8/25/06, Trans <transfire@gmail.com> wrote:
> Hi--

Trans,
would you mind changing the name of this tool to InlineRunner?
There's already a RubyInline, and this would seem to add confusion.

Looks interesting though.

[info deleted]

> Hope you all like and find this useful. It will be included in the next
> release of my build tools.
>
> Thanks,
> T.
>
>

[sample code deleted]

--
thanks,
-pate
-------------------------
http://on-ruby.bl...

Trans

8/26/2006 12:02:00 AM

0


pat eyler wrote:
> On 8/25/06, Trans <transfire@gmail.com> wrote:
> > Hi--
>
> Trans,
> would you mind changing the name of this tool to InlineRunner?
> There's already a RubyInline, and this would seem to add confusion.

Ah, the one that allows C to be run in Ruby. Okay. I'll have to think
of another name then: "ruby___"?

> Looks interesting though.

Thanks. I just realized I shoul dprobably give an example though:

def hello ; "hello"; end

=begin test
class TestHello < Test::Unit::TestCase
def test_hello
assert_equal( 'hello', hello )
end
end
=end

Then

% rubyinline -b test hello.rb

Although in the case, the "-b test" is the default so can be left out.

T.

Trans

8/26/2006 8:48:00 PM

0


Trans wrote:

> I do have one pressing issue though. I came up with two ways to run the
> code. 1) using eval, and 2) using a pipe. The first is better b/c I can
> maintain the line numbers, so that error reports point to the right
> place. OTOH the second way allows all the normal command line options
> that ruby handles to be passed along. I would like to achieve both of
> these rather than one or the other. Both methdos are presented in the
> code below. Any ideas on how to fix?

Wow. No one has any ideas on this? Hmm.. maybe a more specific
question:

Is there away to pass the ruby command a __LINE__ offset? Or set it in
code?

T.

Trans

8/27/2006 4:15:00 PM

0


Marshall T. Vandegrift wrote:
> "Trans" <transfire@gmail.com> writes:
>
> >> I do have one pressing issue though. I came up with two ways to run the
> >> code. 1) using eval, and 2) using a pipe.
>
> [snip]
>
> > Is there away to pass the ruby command a __LINE__ offset? Or set it in
> > code?
>
> You could #eval the code in the subprocess. That seems like an easy (if
> slightly kludgey) way to get the benefits of both methods.

Nice! I'll try that!

Just the sort of creative answer I hoping for. Thanks Marshall.

T.


e

8/27/2006 4:41:00 PM

0

Trans wrote:
> Marshall T. Vandegrift wrote:
>> You could #eval the code in the subprocess. That seems like an easy (if
>> slightly kludgey) way to get the benefits of both methods.
>
> Nice! I'll try that!
>
> Just the sort of creative answer I hoping for. Thanks Marshall.

Marshall's solution should be just fine--however,
I wanted to mention something a bit extraneous: the
library name 'rubyinline' can easily be confused with
the existing Inline libs.

I understand that this is a generalised version, but I
would still recommend using the standard test format by
naming the libs Test::Inline and reside in test/inline.

Not sure what to call it if you want to designate it as
separate from testing--'runcomments', 'comments2ruby' or
something maybe? :)

> T.


--
Posted via http://www.ruby-....

Trans

8/27/2006 4:54:00 PM

0


Eero Saynatkari wrote:
> Trans wrote:
> > Marshall T. Vandegrift wrote:
> >> You could #eval the code in the subprocess. That seems like an easy (if
> >> slightly kludgey) way to get the benefits of both methods.
> >
> > Nice! I'll try that!
> >
> > Just the sort of creative answer I hoping for. Thanks Marshall.
>
> Marshall's solution should be just fine--however,
> I wanted to mention something a bit extraneous: the
> library name 'rubyinline' can easily be confused with
> the existing Inline libs.
>
> I understand that this is a generalised version, but I
> would still recommend using the standard test format by
> naming the libs Test::Inline and reside in test/inline.
>
> Not sure what to call it if you want to designate it as
> separate from testing--'runcomments', 'comments2ruby' or
> something maybe? :)

Understood. I just haven't found a new name I like yet. It was called
'rubytest' but now that it is generalized and can be used for more than
just unit tests, I'm even less sure what to name it. I prefer the
command start with 'ruby___' though because it is really just the ruby
interpretor, but running the script from a different "viewpoint" so to
speak.

T.

Matt Todd

8/27/2006 6:23:00 PM

0

ActiveTest

(Not to steal from the RoR clan, but I like the fact that it's
actively in the same file and sitting there, actively waiting to be
tested.)

Also, I really like InlineRunner, so what about RubyInliner or
RubyIRunner to go with your ruby___. (I don't really agree, but that's
just me.)

Oh, by the way... this looks really, very cool. :)

Cheers!

M.T.

Rob Sanheim

8/27/2006 6:43:00 PM

0

On 8/27/06, Matt Todd <chiology@gmail.com> wrote:
> ActiveTest
>
> (Not to steal from the RoR clan, but I like the fact that it's
> actively in the same file and sitting there, actively waiting to be
> tested.)

There is a Rails plugin called ActiveTest, so that ones taken too =).

http://www.mathewabonyi.com/articles/2006/08/14/activetest-rails-sty...

- rob
--
http://www.robs...
http://www.seekin...
http://www.a...

Matt Todd

8/28/2006 1:15:00 AM

0

> There is a Rails plugin called ActiveTest, so that ones taken too =).

Whoops!

:D

M.T.

Ryan Davis

8/28/2006 7:55:00 AM

0


On Aug 25, 2006, at 4:00 PM, Trans wrote:

> I do have one pressing issue though.

Me too! the name... RubyInline is alive and well-used so please don't
camp on my project's name.

--
ryand-ruby@zenspider.com - http://blog.zens...
http://rubyforge.org/projects/r...
http://www.zenspider.com/...