Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在传递给def-Rails的数组中使用变量_Ruby On Rails_Arrays_Definition_Local Variables - Fatal编程技术网

Ruby on rails 在传递给def-Rails的数组中使用变量

Ruby on rails 在传递给def-Rails的数组中使用变量,ruby-on-rails,arrays,definition,local-variables,Ruby On Rails,Arrays,Definition,Local Variables,我正在使用GoogleCharts API在Rails应用程序中生成饼图。但是,我在将局部变量传递给帮助程序中的def时遇到问题。def采用[label,value]对的2D数组。它不喜欢我尝试将局部变量作为值传入。这些是提前计算的,并且是货币格式。将变量置于引号或{}中也不起作用 应用程序\u helper.rb 视图中的def调用: <%= pie_chart( [["Light Cost", #{light_cost}], ["Small Applian

我正在使用GoogleCharts API在Rails应用程序中生成饼图。但是,我在将局部变量传递给帮助程序中的def时遇到问题。def采用[label,value]对的2D数组。它不喜欢我尝试将局部变量作为值传入。这些是提前计算的,并且是货币格式。将变量置于引号或
{}
中也不起作用

应用程序\u helper.rb 视图中的def调用:

<%= pie_chart( [["Light Cost", #{light_cost}], ["Small Appliance Cost", #{sm_appl_cost}]], :width => 550, :height => 200) %>
550,:height=>200)%>

如何传递局部变量?

您得到的错误是什么?您应该可以这样称呼它:

<%=
  pie_chart(
    [
      ["Light Cost", light_cost],
      ["Small Appliance Cost", sm_appl_cost]
    ],
    { :width => 550, :height => 200 }
  )
%>
550,:height=>200}
)
%>
我在散列中添加了
{}
,只是为了让它更清晰、更明确


在这里使用
{var}
不是一种方法,因为这是在字符串中进行替换,例如
“这里是值:{var}

,这样代码肯定需要数值,但是
{}
只需要开始一个注释,除非在字符串中使用,并且您不需要字符串

事实上,所写的饼图看起来要么像你说的那样是2D数组,要么是散列,如果它只与2变量
.map
迭代器一起使用的话。因此,您应该能够使用
[[“str1”,轻成本],“str2”,sm应用成本]]
{'str1'=>轻成本,'str2'=>sm应用成本}

不过,您确实需要确保这些局部变量是数字。如果没有,请尝试使用
.to\u i
.to\u f

<%=
  pie_chart(
    [
      ["Light Cost", light_cost],
      ["Small Appliance Cost", sm_appl_cost]
    ],
    { :width => 550, :height => 200 }
  )
%>