2

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?

ylluminate
  • 12,102
  • 17
  • 78
  • 152

3 Answers3

4

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:

  • Ruby on Rails is a very common framework many beginners often start with.

  • Sinarta is also a good starting point, usually favored by more experienced coders who tend to believe it's less heavy than Rails.

  • Using Rack directly (with no framework) is the hi-performance mainstream choice, but it requires more knowledge.

  • Read the following link for more common Http-Web frameworks and Benchmarks.

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

Myst
  • 18,516
  • 2
  • 45
  • 67
0

Yep, there's a templating system known as embedded ruby (erb). In here, you can place ruby code inside html tags. Ruby code is embedded with a pair of <% and %> delimiters. However, to output the value to the view, the first pair needs to be <%=

Here's a simple example.

<ul>
  <% 3.times do %>

    <li>list item</li>

  <% end %>
</ul>

Your files are typically named like index.html.erb

Here's much more information on the subject

https://en.wikipedia.org/wiki/ERuby

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
0

This might be a good page get the beginning ideas of how to implement RoR. Here's a snippet from the page. As you can see you can do RoR right inline in HTML with the <% and %> beginning and ending tags.

<%= form_for :article do |f| %>
  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>
god_is_love
  • 571
  • 5
  • 18
  • Always bearing in mind, of course, that while Rails _might_ be part of an answer for the questioner, the actual question has nothing to do with Rails... – Andy Jones Dec 03 '15 at 11:35