DEV Community

Samuel Lubliner
Samuel Lubliner

Posted on

1

Dynamic path segments

We improved the code for our dice rolling web app by adding support for all possible combinations of number of dice and type.

We refactored the app.rb code with 1 addition and deleting 42 delitions!

#.rb

get("/dice/:number_of_dice/:how_many_sides") do

  @num_dice = params.fetch("number_of_dice").to_i

  @sides = params.fetch("how_many_sides").to_i

  @rolls = []

  @num_dice.times do
    die = rand(1..@sides)

    @rolls.push(die)
  end

  erb(:flexible)
end
Enter fullscreen mode Exit fullscreen mode

.erb


<h1><%= @num_dice %>d<%= @sides %></h1>

<ul>
  <% @rolls.each do |a_roll| %>
    <li>
      <%= a_roll %>
    </li>
  <% end %>
</ul>

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs