htmlcssrubyopalrb

Linking Ruby with HTML Code


I am aware that you can implement Javascript code to an HTML documents, by inserting the code between the <script></script> tags.. is there a similar way to do this with Ruby?


Solution

  • Client Side Ruby (Ruby on the browser?!)

    If your question refers to executing Ruby code on the browser, like you would execute Javascript (the server isn't involved), than it's not really possible in the traditional sense.

    However, Opal (or similar transpilers) will "compile" your Ruby code into Javascript, allowing you to execute the compiled "Ruby" (now javascript) code within the browser, just like native Javascript (sometimes even better, as Opal attempts to optimize your code).

    Server Side Ruby

    Ruby is often used to run code on the server rather than the browser. There are a number of solutions.

    For example, you can use ERB templates - here's a good intro to ERB - as well as other Ruby template engines (i.e. the amazing Slim template engine) to run Ruby code in a similar way to PHP code (only easier to write and maintain).

    However, Ruby is much more powerful than simple templates. There are whole servers and frameworks written in and for Ruby, allowing you very powerful server-side scripting.

    The common frameworks:

    There are also more experimental, or less common, frameworks - usually ones focusing on real-time web applications, such as Volt and Plezi.