toddmetheny
toddmetheny

Reputation: 4453

Running code in the browser

How does codewars (and other similar sites) run the code in the browser? I’m sure they are just running it on their own server and returning the values. But how does it work? Like they write that code to a temporary file or something, run it on their server…then return the values? And how? Simpler than I’m making it?

I read this: https://www.quora.com/How-do-sites-like-CodeCombat-CodeCademy-and-CodeWars-safely-execute-and-test-code-entered-by-the-users-What-would-their-architecture-look-like

That addresses the security piece, but I still feel unsatisfied. I was hoping someone might have something better to add here about how it actually works.

Upvotes: 3

Views: 135

Answers (1)

Anthony
Anthony

Reputation: 15987

This is total speculation, I really don't know what they do but you can execute strings in ruby [in a not very safe way] by using Kernel#eval . Take this as an example:

eval "class MyClass; def run; puts 'I ran it!'; end; end; MyClass.new.run;"
=> I ran it!

So in theory, you could perform substitutions on the string (i.e. \n becomes ;) and run it through eval.

Upvotes: 2

Related Questions