Reputation: 4453
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?
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
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