# How can I evaluate and visualize code instead of running it

**URL:** <https://www.ruby-forum.com/t/how-can-i-evaluate-and-visualize-code-instead-of-running-it/71214>\
**Category:** Ruby\
**Created:** [February 21, 2006, 12:49pm UTC](https://www.ruby-forum.com/t/how-can-i-evaluate-and-visualize-code-instead-of-running-it/71214 "2006-02-21T12:49:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 12:49pm UTC](https://www.ruby-forum.com/t/how-can-i-evaluate-and-visualize-code-instead-of-running-it/71214/1 "2006-02-21T12:49:22Z")

</div>

I am using a tree and traversing down a tree to generate opengl code  
How could I easily list the opengl code that I am generating, instead  
of running it?

def applytransform  
GL.PushMatrix();  
GL.Translate(_@Translation);  
GL.Translate(_@jointP);  
if @parent != nil  
GL.Translate(_@parentjointP);  
end  
GL.Rotate(_@rotation);  
GL.Translate(\*@jointP.minus);  
end

def traversetree  
GL.Clear(GL::COLOR\_BUFFER\_BIT);  
GL.PushMatrix();  
applytransform  
@children.each {|childnode| childnode.traversetree}  
drawprimitive  
GL.PopMatrix();  
GLUT.SwapBuffers();  
end

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/how-can-i-evaluate-and-visualize-code-instead-of-running-it/71214/2 "2006-02-21T15:10:41Z")

</div>

“anne001” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) schrieb im Newsbeitrag  
news:removed\_email\_address@domain.invalid…

> end  
> GL.PopMatrix();  
> GLUT.SwapBuffers();  
> end

I’d refactor the traversal out of the code and encapsulate actions on  
tree  
nodes in blocks or a specific visitor class. In your case a specific  
class  
might be better because you seem to need entry and exit code. Something  
like:

…  
def traversetree(visitor)  
visitor.entry[self]  
@children.each {|childnode| childnode.traversetree(visitor)}  
visitor.exit[self]  
end  
…

GLVisitor = Struct.new(:entry, :exit)

code\_runner = GLVisitor.new( lambda do |n|  
GL.Clear(GL::COLOR\_BUFFER\_BIT)  
GL.PushMatrix()  
n.applytransform  
end,  
lambda do |n|  
n.drawprimitive  
GL.PopMatrix();  
GLUT.SwapBuffers();  
end)

code\_lister = GLVisitor.new( lambda …

HTH

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/how-can-i-evaluate-and-visualize-code-instead-of-running-it/71214/3 "2006-02-21T21:19:19Z")

</div>

Thank you Robert. I can see that it would be cleaner to  
separate the code from the traversing of the code. And I  
see how you did it.

But I don’t see how it helps, how does “code\_lister” list what is in  
"code\_runner? "

anne

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/how-can-i-evaluate-and-visualize-code-instead-of-running-it/71214/4 "2006-02-21T17:19:08Z")

</div>

Hi,

From: “anne001” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> end  
> GL.PopMatrix();  
> GLUT.SwapBuffers();  
> end

Maybe one possibility:

require ‘opengl’

RealGL = GL

class LoggingGL  
class \<\< self  
def method\_missing(meth, \*args)  
puts “#{meth}(#{args.join(', ')})”  
RealGL.send(meth, \*args) # remove this line if you don’t want to  
really call GL  
end  
def const\_missing(const)  
RealGL.const\_get(const)  
end  
end  
end

GL = LoggingGL

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

The above will turn this:

GL.MatrixMode(GL::PROJECTION)  
GL.LoadIdentity  
GL.Scaled(1.5, 1.2, 1.0)

Into the following output:

MatrixMode(5889)  
LoadIdentity()  
Scaled(1.5, 1.2, 1.0)

Hope this helps,

Bill

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 21, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/how-can-i-evaluate-and-visualize-code-instead-of-running-it/71214/5 "2006-02-21T22:36:21Z")

</div>

Thank you very much Bill K. for your response.

The Key is that you are swapping what GL means. If I say  
GL=LoggingGL  
suddenly, I am no longer calling on OpenGL, I am calling on methods in  
loggingGL

I tried it as it, as it is very easy to tag this little code to modify  
the fx. The glut fx  
was not modified, and it opened a window with the strangest mix of  
text/image.  
I could fix that by redirecting glut I think.

This will work very well. Thank you for thinking of this scheme and  
taking the time to  
write it all out so it works right away

thank you

Anne

---

<div class="post-metadata">

**Author:** ![anne001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/34f0e0/32.png) [@anne001](https://www.ruby-forum.com/u/anne001)\
**Post date:** [February 22, 2006, 11:30am UTC](https://www.ruby-forum.com/t/how-can-i-evaluate-and-visualize-code-instead-of-running-it/71214/6 "2006-02-22T11:30:05Z")

</div>

2006/2/21, anne001 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thank you Robert. I can see that it would be cleaner to  
> separate the code from the traversing of the code. And I  
> see how you did it.
> 
> But I don’t see how it helps, how does “code\_lister” list what is in  
> "code\_runner? "

You’ll have to separate the generation code from the invocation code.  
That way you can reuse the generation code in the lister and simply  
print it instead of invoking it.

I don’t know GL so take this advice with a grain of salt.  
It was a general advice about separating concerns  
properly.

HTH

robert
