2

I'm implementing a kind of ruby online terminal (just for practice). I am using AJAX from a simple js web application to call a method that evals the input of the "terminal". To avoid problems with the environment and let the user creates his own objects, i was using a "binding" object. The problem is that i don't know how to persist the bindings.

My first idea was to create a bindindg that persists between posts requests for each time a user access to the application. In that moment, a key-object pair is created with a unique key and a new binding. The key is then passed to the client. When the user sends the string to evaluate via ajax, i send the key. Then, i can get the binding object and update it.

I tried something like:

class TerminalController
    @@bindings = {}

    def index
        @token = keyToken
        @@bindings[@token] = getBinding
    end

    private

        def keyToken
            Digest::MD5.hexdigest(Time.now.to_s)
        end

        def getBinding
            # declare methods for all bindings
            # ...
            binding
        end
end

But in execution time, the code fails creating the key-value pair. In fact, anything that i try to execute after that line fails. I don't understand why.

Actually, i think if i can serialize the binding it will work, but i think it can be inefficient because the network latency and the posible growth of the binding.

1 Answers1

0

Unfortunately I don't think there is a way to serialize a binding in most Ruby implementations. If you try to Marshal.dump a binding you get a TypeError in MRI (Marshal is the built-in library for serializing objects in Ruby).

There may be more other ways of doing what you're trying to do. However, if you must serialize a binding then take a look at MagLev. That implementation is designed so that any object can be persisted.

Simon Chiang
  • 835
  • 1
  • 8
  • 14
  • i actually tried to do the work in Rubinius. The Marshal.dump seemed to works, but the serialized string didn't contains information about any variable or method, so it was useless. I will try MagLev and i hope it will work. Thanks. I actually wish a ruby online repl. –  Nov 08 '11 at 01:34
  • 1
    A different approach might be using websockets, which can persist a connection with a server. When you are done with your repl session, you close the connection and that's it. I heard of something like this once... this may be it but I've never tried it myself: https://github.com/cldwalker/nirvana – Simon Chiang Nov 08 '11 at 20:11