Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7174

Re: getting in between results of chunks of ruby code, by how???

Newsgroups comp.lang.ruby
Date 2015-12-23 01:37 -0800
References <5fc33a05-0c6f-4082-a815-3dc044b13cff@googlegroups.com> <ddu37fFlkt1U1@mid.individual.net>
Message-ID <b368625d-aa48-44e2-a0bf-642e0975223f@googlegroups.com> (permalink)
Subject Re: getting in between results of chunks of ruby code, by how???
From artbot <am@artbot.de>

Show all headers | View raw


On Wednesday, December 23, 2015 at 12:04:27 AM UTC+1, Robert Klemme wrote:
> On 22.12.2015 21:13, artbot wrote:
> > hi all,
> >
> > i've a problem with evaluating ruby chunks in an array. the setting is as follows (and may seem exotic, but anyway):
> >
> > let start with an array 'arr'
> >
> > arr= ["a= rand(4)+2\na.times do |i|","c= i*i\nc","end","b=a*a\nb"]
> >
> > the array 'arr' contains a valid ruby program, which can be run using:
> >
> > eval(arr.join)
> >
> > so far so easy. but i need to get the evaluation of the in between steps also as subresults.
> >
> > starting with the above array 'arr', i'd like to get (for 'a' being 3)
> >
> > brr= [nil, [0, 1, 4], nil, 9]
> >
> > so each ruby code chunk should store it's last value (can be nil)
> > at the same array index as the original ruby chunk.
> >
> > so e.g. as
> >
> > "a= rand(4)+2\na.times do |i|"
> >
> > doesn't have a last value, therefore brr[0] is nil.
> >
> > "c= i*i\nc" is called 3 times, therefore brr[1] is
> > an array of size 3 with the corresponding results
> >
> > "end" doesn't have a value, therefore brr[2] is nil
> >
> > brr[3] is 9, as 3*3 is 9
> >
> > any idea how this can be done with eval or something else in ruby???
> 
> Sounds like you were trying to create a debugger.  You might get some 
> inspiration from what "require 'debug'" loads.
> 
> You could also use tools / gems to parse Ruby into an AST and work on 
> that (e.g. interpret the code).
> 
> Kind regards
> 
> 	robert
> 
> 
> -- 
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestpractices.com/

it's going to be part of my (very specialized) template engine.

it should be as code/ruby agnostic as possible. evaluating each block
should just recognize if it produces a result or not. also repetitions
should get recognized (as above)

i also thought about a similar scheme as in 
https://github.com/soveran/mote/blob/master/lib/mote.rb
but my other (non code) objects are not mere strings, but pdf drawable ruby objects (generated by a parser (= kpeg), Prawn::Document is my backend)

any more ideas?

cheers

artur

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

getting in between results of chunks of ruby code, by how??? artbot <am@artbot.de> - 2015-12-22 12:13 -0800
  Re: getting in between results of chunks of ruby code, by how??? Robert Klemme <shortcutter@googlemail.com> - 2015-12-23 00:04 +0100
    Re: getting in between results of chunks of ruby code, by how??? artbot <am@artbot.de> - 2015-12-23 01:37 -0800
      Re: getting in between results of chunks of ruby code, by how??? Robert Klemme <shortcutter@googlemail.com> - 2015-12-23 11:14 +0100
        Re: getting in between results of chunks of ruby code, by how??? artbot <am@artbot.de> - 2015-12-23 05:49 -0800
          Re: getting in between results of chunks of ruby code, by how??? artbot <am@artbot.de> - 2015-12-23 11:02 -0800

csiph-web