Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.ruby Subject: Re: getting in between results of chunks of ruby code, by how??? Date: Wed, 23 Dec 2015 00:04:14 +0100 Lines: 51 Message-ID: References: <5fc33a05-0c6f-4082-a815-3dc044b13cff@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net MOQpWI8idPOYG1w/+ylHRgJVgbhLyoa0Om5ZWATReoA1lZ63E= Cancel-Lock: sha1:/aBtRQvkXeFVI24PSKkd6yw/5cg= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 In-Reply-To: <5fc33a05-0c6f-4082-a815-3dc044b13cff@googlegroups.com> Xref: csiph.com comp.lang.ruby:7173 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/