rubyinline

T

Trans

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
 
P

pat eyler


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]
 
T

Trans

pat said:
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.
 
T

Trans

Trans said:
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.
 
T

Trans

Marshall said:
Trans said:
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

Eero Saynatkari

Trans said:
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

Trans

Eero said:
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.
 
M

Matt Todd

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.
 
T

Trans

Eric said:
Did you typo rubygems?

Okay. I'll take that as a 'no'.

I played around with some more names. 'rubyinlay' came to mind, and I
almost settled on 'rubydemo', but that strays too far from test usage I
think, so now I'm thinking 'rubyhand', as in a helping hand and the
comment block handle.

T.
 
L

Logan Capaldo

Okay. I'll take that as a 'no'.

I played around with some more names. 'rubyinlay' came to mind, and I
almost settled on 'rubydemo', but that strays too far from test
usage I
think, so now I'm thinking 'rubyhand', as in a helping hand and the
comment block handle.

T.

Ok I realize the ruby community has this penchant for clever names,
but why not call it runcomments or similar?
 
T

Trans

Logan said:
Ok I realize the ruby community has this penchant for clever names,
but why not call it runcomments or similar?

That's a fair point. Although you might be interested in this:


http://reverendted.wordpress.com/2006/08/30/products-and-projects-whats-in-a-name/

Yet, in this case, becasue of it's specific application, it might
nonethless be a god idea to use a descriptive name. Tell you what...
what I really wish I could do is add an option (-h perhaps) to 'ruby'
itself. That would be the bomb.

Well, I'll think on it some more.

Thanks,
T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top