Rubyinline

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

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” [email protected] 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.

-Marshall

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.

T.

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? :slight_smile:

T.

Eero S. wrote:

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? :slight_smile:

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.

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. :slight_smile:

Cheers!

M.T.

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

Whoops!

:smiley:

M.T.

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.

On 8/27/06, Matt T. [email protected] 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-style-testing

  • rob

Trans wrote:

Ryan D. wrote:

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.

Okay (for the third time).

OKay, I think I’ll call it RubyEmbedded with the shell command named
‘rubyem’. That work for everyone?

Ryan D. wrote:

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.

Okay (for the third time).

T.

On Aug 28, 2006, at 8:25 AM, Trans wrote:

Okay (for the third time).

OKay, I think I’ll call it RubyEmbedded with the shell command named
‘rubyem’. That work for everyone?

Did you typo rubygems?


Eric H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

Eric H. wrote:

camp on my project’s name.

Okay (for the third time).

OKay, I think I’ll call it RubyEmbedded with the shell command named
‘rubyem’. That work for everyone?

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.

On 8/29/06, Trans [email protected] wrote:

Eric H. wrote:

On Aug 28, 2006, at 8:25 AM, Trans wrote:

OKay, I think I’ll call it RubyEmbedded with the shell command named
‘rubyem’. That work for everyone?

Did you typo rubygems?

Okay. I’ll take that as a ‘no’.

For some reason rubyem made me think of Dorothy, Tinmen, and Scarecrows.

You know, Ruby Slippers, Auntie Em

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Rick DeNatale wrote:

For some reason rubyem made me think of Dorothy, Tinmen, and Scarecrows.

You know, Ruby Slippers, Auntie Em

LOL. :smiley: That’s funny.

T.

On Aug 29, 2006, at 9:05 PM, Trans wrote:

Me too! the name… RubyInline is alive and well-used so please
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?

Logan C. wrote:

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.

On 8/30/06, Trans [email protected] wrote:

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.

How about ‘rubyiunit’ or ‘rubyitest’? To reflect both the fact that the
unit tests are inlined into the source file, and that they are in fact
unit
tests.

  • Dimitri

On Aug 29, 2006, at 6:05 PM, Trans wrote:

Eric H. wrote:

On Aug 28, 2006, at 8:25 AM, Trans wrote:

OKay, I think I’ll call it RubyEmbedded with the shell command named
‘rubyem’. That work for everyone?

Did you typo rubygems?

Okay. I’ll take that as a ‘no’.

I don’t know, but it is a small typo away. Names are hard.


Eric H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com