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

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]

···

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

--
thanks,
-pate
-------------------------

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.

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

···

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

I do have one pressing issue though.

--
ryand-ruby@zenspider.com - http://blog.zenspider.com/
http://rubyforge.org/projects/rubyinline/
Seattle.rb | Home

pat eyler 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.

···

On 8/25/06, Trans <transfire@gmail.com> 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.

-Marshall

Ryan Davis 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.

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.

Trans wrote:

Ryan Davis 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?

···

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

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.

--
Posted via http://www.ruby-forum.com/\.

Did you typo rubygems?

···

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

Trans wrote:

Ryan Davis 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?

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

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

Eric Hodel wrote:

···

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

> Trans wrote:
>> Ryan Davis 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?

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.

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.

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 8/30/06, Trans <transfire@gmail.com> 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.

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

You know, Ruby Slippers, Auntie Em <G>

···

On 8/29/06, Trans <transfire@gmail.com> wrote:

Eric Hodel 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'.

--
Rick DeNatale

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

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

···

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

Eric Hodel wrote:

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

Trans wrote:

Ryan Davis 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?

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.

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

···

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

Eric Hodel 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'.

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

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

···

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

--

http://www.ajaxian.com

Rick DeNatale wrote:

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

You know, Ruby Slippers, Auntie Em <G>

LOL. :smiley: That's funny.

T.