alexgutteridge/rsruby — Issue #12: Caching in rsruby is causing havoc and incorrect results...
Author: kishorerathi
Created: 2011-12-21 16:09:14+00:00
State: open

There is a caching feature in rsruby and it seems to be on by default and there is no way to turn it off. This feature is causing wrong results when ever variables are changed in R but the old values are persisted and returned in rsruby. The following code will explain the problem...

###################################################################
r = RSRuby.instance

r.assign('p', 21)
r.assign('r', 121)
r.assign('R', 221)
printf "%d %d %d\n" % [r.p, r.r, r.R] #### Prints 21, 121, 221

p 'Assigning a new value...'
r.assign('R', 3221)
printf "%d %d %d\n" % [r.p, r.r, r.R] #### Still prints 21, 121, 221 ===> value of r.R is wrong

p 'deleteing from cache..' ##### This is a hack as it forces rsruby to read from R
r.delete_from_cache('R')
printf "%d %d %d\n" % [r.p, r.r, r.R] ##### Prints correctly 21, 121, 3221

p 'removing....' ##### Remove variable R and r from R
r.rm('R', 'r')
p 'still printing....'
printf "%d %d %d\n" % [r.p, r.r, r.R] ##### But rsruby still prints r and R which is wrong again

p "Deleted from cache, and erroring...."
['R', 'r'].each {|i| r.delete_from_cache(i)}
printf "%d %d %d\n" % [r.p, r.r, r.R] #### Once flushed from cache, the code errors out appropriately

##########################################

As we can see, to get the correct behavior we have to flush the cache manually and this shouldn't be required. In essence caching of R variables is not appropriate in rsruby space. At least the data variables.

Please let me know if I am doing anything wrong in the above sample code.

Thanks

K