为什么这个变量不显示在 Sinatra 的页面上

Why doesn't this variable display on page in Sinatra

试图显示 @total 变量,但它总是显示 0

让它显示在页面上的正确方法是什么?

目前我正在做...

main.rb

require 'sinatra'

get '/' do

  @array = [1, 7, 3, 0]
  @index = params[:index].to_i
  @total = 1

  def get_products_of_all_ints_except_at_index()
    @array.delete_at(@index)
    @array.each do |i|
      @total *= i
    end
  end
  get_products_of_all_ints_except_at_index()
erb :home
end

home.erb

<form action="/">

  <input type="number" name="index" placeholder="index">

  <button>Calculate</button>

</form>

<%= @total %>

在您的 @array 中,最后一个元素是 0。因为您正在对其进行迭代并将总计与每个元素相乘,所以总计也是 0