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


Groups > comp.lang.ruby > #7173

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

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: getting in between results of chunks of ruby code, by how???
Date 2015-12-23 00:04 +0100
Message-ID <ddu37fFlkt1U1@mid.individual.net> (permalink)
References <5fc33a05-0c6f-4082-a815-3dc044b13cff@googlegroups.com>

Show all headers | View raw


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/

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