8

From the Snippets Announcement

Why?

Every question is better for having minimal, reproducible code.

...

Similarly, answers that include runnable code are easier to use and understand, because you can try them out

I see this question has been asked for specific other languages suggesting specific solutions.

It seems like it would be nice to support running code from languages other than JavaScript in snippets.

Various other online sites support C++, Haskell, Lua, Ocaml, Perl, Python, Ruby, Scheme, Java, etc...

Or as a start even just other JavaScript transpilers. Babel is in there now for ES6. How about Typescript, Flow, Clojurescript, etc..

8
  • 1
    This was asked before and decilned. I'm pretty sure. Commented Jul 11, 2016 at 18:22
  • 6
    Wait, you've seen this question asked for specific languages, and so decided to add one for all languages? Even though in order to support this, each language would have to be handled different? Wut?
    – user1228
    Commented Jul 11, 2016 at 18:28
  • You forgot Forth. Commented Jul 11, 2016 at 20:06
  • 1
    I'd like to have a snippet runner for malbolge please. Can we have that pleaaaaase, is think it's extremely important for questions about that language. Commented Jul 11, 2016 at 20:16
  • @Will, they've already added babel support which means they're already doing translation of the text. No reason to stop there. The reasons snippets were added in the first place are listed at the top of the question. If those reasons don't apply to other languages then they also don't apply to JavaScript and snippets should be removed. Otherwise other languages should be supported for the same reasons.
    – gman
    Commented Jul 11, 2016 at 22:47
  • @gman but... each one would have to be handled separately, as they're different languages. And they would have to be prioritized. And, at some point, you have to stop adding language support. A general question is useless for these reasons.
    – user1228
    Commented Jul 12, 2016 at 12:51
  • Cross-site post on Meta.SE: Stack Snippets for PowerShell, Python, etc. Commented Jun 4, 2019 at 1:02
  • @πάνταῥεῖ You still want malbolge? tio.run/#malbolge ;)
    – PM 2Ring
    Commented Aug 20, 2021 at 9:24

3 Answers 3

8

As I said before (for Python specifically), you could use the currently available features within Stack Snippets to get something similar:

<script src="//repl.it/embed/Caky/1.js"></script>

Of course, you should never just leave it at that. You need to provide the real code, not just a link. This is Ruby:

print "World, hello!"

a = [ 45, 3, 19, 8 ]
b = [ 'sam', 'max', 56, 98.9, 3, 10, 'jill' ]
print (a + b).join(' '), "\n"
print a[2], " ", b[4], " ", b[-2], "\n"
print a.sort.join(' '), "\n"
a << 57 << 9 << 'phil'
print "A: ", a.join(' '), "\n"

b << 'alex' << 48 << 220
print "B: ", b.join(' '), "\n"
print "pop: ", b.pop, "\n"
print "shift: ", b.shift, "\n"
print "C: ", b.join(' '), "\n"

b.delete_at(2)
b.delete('alex')
print "D: ", b.join(' '), "\n"

Now, before you say "but I want to force them to make an MVCE", let me tell you that this will not work as a solution. They will find a way to screw it up:

print "https://repl.it/Caky/1"

Half the time when a new user uses the feature as it is, the snippet is actually a Java snippet. Or it fails to compile. Or it compiles, but nothing shows up.

JS/HTML/CSS are native to the browser, so our current system easily accommodates them. Most, if not all of these other online testers fail to work with anything graphical, which was one of the biggest advantages of Stack Snippets in the first place.

In the end, this trick is really only useful for answerers who want an edge. I wouldn't overdo it, as I personally feel this has the potential to become a broken link or a security vulnerability.

8

Well, just a few thoughts:

  • How would you address compiler specific behavior coming along with tags like or ?
  • It's already possible to post MCVE's and refer to appropriate Online IDE's as listed from tag wiki's.
  • Snippets are annoyingly used already, where they simply don't apply, wrong programming language or not.

I don't think it's necessary/wanted to add runnable code snippets support for more languages.

4

It looks like Lua can be supported by using Fengari, a Lua VM implemented in JS.

So taking Laurel's Ruby snippet example and turning it into Lua:

<script src="//github.com/fengari-lua/fengari-web/releases/download/v0.1.4/fengari-web.js"></script>

<script type="application/lua">
local table = require 'table'

table.add = function(a, b)
  local c = {}
  for _, v in ipairs(a) do c[#c + 1] = v end
  for _, v in ipairs(b) do c[#c + 1] = v end
  return c
end

table.find = function(t, key)
  for i, value in ipairs(t) do
    if key == value then return i end
  end
  return nil
end

print "World, hello!"

a = { 45, 3, 19, 8 }
b = { 'sam', 'max', 56, 98.9, 3, 10, 'jill' }

print ( table.concat(table.add(a, b), ' '), "\n" )
print (a[2], b[4], b[#b - 2], "\n")
table.sort(a)
print (table.concat(a, ' '), "\n")

table.insert(a, 57)
table.insert(a, 9)
table.insert(a, 'phil')
print ("A: ", table.concat(a, ' '), "\n")

table.insert(b, 'alex')
table.insert(b, 48)
table.insert(b, 200)
print ("B: ", table.concat(b, ' '), "\n")
print ("pop: ", table.remove(b), "\n")
print ("shift: ", table.remove(b, 1), "\n")
print ("C: ", table.concat(b, ' '), "\n")

table.remove(b, 2)
table.remove(b, table.find(b, 'alex'))
print ("D: ", table.concat(b, ' '), "\n")

print "Fengari enables Lua!!"
</script>

This is great. Got one of my favorite scripting languages working with SO snippets :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .