Why can't ruby just compile to erlang's VM?

:sweat_smile: (i hope this doesn’t sound spammy)

1 Like

Because they are very different. Ruby is an OOP language, isn’t concurrent, isn’t immutable etc

The closest thing to getting anything like Ruby on the Erlang VM …is Elixir :icon_wink: :003:


If you are a newbie I highly recommended any one of the brilliant Elixir books out there - they will help you understand a lot about Elixir and even the other technologies that are connected in some way or another :023:

6 Likes

The sooner you realize that Elixir has nothing to do with Ruby the better. Mentally separate them entirely. The semantics of the languages are entirely different. Elixir is just Erlang with different skin on it.

4 Likes

I think it would be totally possible to do so, but it is not worth the effort.

Ruby as an OO language is all about mutabililty, which is strictly forbidden by the BEAM.

Also the dynamic dispatch of methods in classes or instances that had been altered during runtime might be a real hell and slow down the system massively.

1 Like

you could build it but it will be complex and run slower than Ruby

2 Likes

I think it would be totally possible to do so

This does not seem like a substantiated claim to me, and I think it undersells how fundamentally different Ruby is from BEAM languages. I’m sure you could build a Ruby interpreter in Elixir but that really isn’t the same thing.

1 Like

Ruby and Elixir (or Erlang) are conceptually completely different. It’s entirely possible to make Ruby compiled to BEAM, but this compiled ruby would be really slow and also very memory hungry due to the dynamic nature of ruby, which would lead to big abstraction layer. Abstracting memory sharing language on “immutable” virtual machine is not a small task, and also in my opinion totally not needed.

I think that we would benefit much more with speed optimization in current Ruby VM.

2 Likes

This should be mandatory reading for everyone new to Elixir.

3 Likes

:thumbsup: I second that!

Ruby is Turing Compilers as well as BEAM. So I do not see any reason why it weren’t possible.

Of course an interpreter might be much easier, but I can’t see anything that would disprove nigh claim in general.

1 Like

It being Turing complete just means you could write an interpreter in elixir or erlang. That isn’t the same thing as saying it can compile to VM byte code.

You can’t compile ruby to ASM either but that doesn’t mean ASM isn’t Turing complete.

1 Like

I think that’s a bit harsh tbh :lol:

Elixir’s syntax was, in part, inspired by Ruby, and Phoenix itself inspired by Rails. Many (the majority?) of early adopters also came from Ruby and Rails. This is all well documented in talks that both José and Chris have given on what prompted them to create these technologies. Ruby (and Rails) are definitely worthy tech to be inspired by imo :023:

Having said that, yes, both Elixir and Phoenix are also very different to their Ruby counterparts.

That’s much more agreeable :003:

2 Likes

Can you proof the impossibility to compile ruby to ASM? I think it is
possible too, but totally in economically mostly because the dispatching of
methods.

To be honest that’s completely wrong. What you write in any programming language is in the end executed as machine language, a lower level then ASM. So there’s nothing stopping you to compile Ruby to BEAM compatible bytecode or ASM… short of the sheer amount of the work you would need to invest in it. And by the way, I think writing transpiler from ruby to ASM would be easier than Ruby to BEAM bytecode compiler.

1 Like

The operative word is “compile”. Compiling something is not the same as running it.

I think I’m going to bow out of this discussion. It isn’t clear we mean the same thing by our terms, and the overall tone has gotten a bit dogmatic.

1 Like

I think what @benwilson512 means it that you will have to have some runtime interpreter/environment for at least part of the code. So you can not compile all of it down to ASM.

1 Like

Indeed, having come from Ruby, and hearing about how Elixir has “Ruby-like syntax”, I was hoping for it to be more like Ruby, at least syntactically. (Not that I’m disappointed or any such thing with Elixir! I like it, there’s just more of a learning curve than I had been led to believe, with lots of things that simply have no parallel in Ruby – or much else that I already knew.) Perhaps some keywords are the same, and in some ways some of the large-scale structure. It seems to me more like the main thing it copies is the approach of valuing programmer-friendliness over things like absolute purity in its paradigm (Ruby in OO, Elixir in FP).

BTW, one little “aha” moment linking Elixir and Ruby (or other typical OO languages), was the realization that Elixir’s |> is very much like the other languages’ .. Frex, in Ruby one might say "foobar".upcase.reverse, while in Elixir, at least after importing String, it’s simply "foobar" |> upcase |> reverse

1 Like

As others have mentioned you can’t directly compile Ruby down to the BEAM (erlang VM) as the underlying semantics are very different and quite incompatible, and the BEAM very specifically targets Erlang semantics. It’s Erlang all the way down. :grinning:

What you could do however, is implement Ruby in Erlang. I have done it for Lua; it’s called luerl, and is complete implementation of Lua 5.2. Seeing we have to implement Lua’s global, shared, mutable data in Erlang it is slower than a native implementation but the interface between the languages is very fast. And you get all of Erlang’s concurrency and system building for free. You could run millions of connections implemented in Ruby on one machine.

It depends on what you are after.

You can replace erlang with elixir in the discussion above as they have the same semantics. They just look different.

9 Likes

Hey Ben, if you feel anyone is holding incorrect assumptions please help us understand why you think they are wrong. I’m sure we’d all rather be corrected than remain ignorant and continue to spread (perhaps inadvertently - even with the best of intentions) misinformation. I’m not sure about anyone else but I’d rather know (and promote) the facts.

However, no problem if you are truly done with this thread :lol:

1 Like

I’ve marked Robert’s post as the accepted answer/solution. Well, he is an authority on the topic given he is one of the language’s creators :003:

I hope this is ok with everyone.

*runs

4 Likes