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.

6 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