DEV Community

petersjang1993
petersjang1993

Posted on

Learning About the Ruby

Learning about Ruby and HTML separately is one thing but when you have to figure out how to combine it together, it can get pretty dicey. As for today, we have learned how to embed the ruby syntax header into the HTML.

For example, I remember the time when I learned how to write for rock paper scissors. In this file, it is normally supposed to be scissors.html. Since now we are also embedding Ruby elements, I had got my first exposure to <% %> elements. We can see that the syntax inside the <% %> are all Ruby elements otherwise the HTML would not be able to read them. And when we save a file like this, we also add scissors.html.erb as well to indicate we have added the Ruby elements.

<% index = rand(3) %>

<% moves = ["rock", "paper", "scissors"] %>

<% comp_move = moves.at(index) %>

<% comp_move = moves.at(index) %>

<% if comp_move == "rock" %>
  <% outcome = "lost" %>
<% elsif comp_move == "paper" %>
  <% outcome = "won" %>
<% elsif comp_move == "scissors" %>
  <% outcome = "tied" %>
<% end %>

<h2>We played scissors!</h2>

<h2>They played <%= comp_move %>!</h2>

<h2>We <%= outcome %>!</h2>

<a href="https://rps-rcav.matchthetarget.com/">
Enter fullscreen mode Exit fullscreen mode

Top comments (0)