Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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每个都做变量替换_Ruby_Each - Fatal编程技术网

Ruby每个都做变量替换

Ruby每个都做变量替换,ruby,each,Ruby,Each,这是each循环的内容,names.each do | x |。我需要用#{x}替换v02,但语法有问题。请告诉我如何用x替换v02 <% @a = Count.find_by_user_id(@user) %> <% @b = @a.v02 %> <% @c = @b * 1.0 %> <% @d = Carpart.find_by_part("v02") %> <% @e = @d.requirement %> <% @f =

这是each循环的内容,names.each do | x |。我需要用#{x}替换v02,但语法有问题。请告诉我如何用x替换v02

<% @a = Count.find_by_user_id(@user) %>
<% @b = @a.v02 %>
<% @c = @b * 1.0 %>
<% @d = Carpart.find_by_part("v02") %>
<% @e = @d.requirement %>
<% @f = @c / @e %>
<% @g = @f * 100 %>
<% @h = [@g, 100].min %>
<% @i = Percentage.find_by_user_id(@user) %>
<% @i.update_attribute(:v02, @h) %>
V02 <%= @i.v02 %>%
<% @v02 = @i.v02 %>

V02%

您可以使用Ruby的
send

<% @b = @a.send(x) %>



我更喜欢后者,只是因为您通常不必担心有人会覆盖它。

您可以使用Ruby的
send

<% @b = @a.send(x) %>



我更喜欢后者,只是因为你通常不必担心有人会凌驾于后者之上。

不完全确定这是否是你的意思,但是。。。你是说这样吗

<% @a = Count.find_by_user_id(@user) %>
<% @b = @a.send(x) %>
<% @c = @b * 1.0 %>
<% @d = Carpart.find_by_part("#{x}") %>
<% @e = @d.requirement %>
<% @f = @c / @e %>
<% @g = @f * 100 %>
<% @h = [@g, 100].min %>
<% @i = Percentage.find_by_user_id(@user) %>
<% @i.update_attribute(x.to_sym, @h) %>
x <%= @i.send(x) %>%
<% @x = @i.send(x) %>

x%

不完全确定这是不是你的意思,但是。。。你是说这样吗

<% @a = Count.find_by_user_id(@user) %>
<% @b = @a.send(x) %>
<% @c = @b * 1.0 %>
<% @d = Carpart.find_by_part("#{x}") %>
<% @e = @d.requirement %>
<% @f = @c / @e %>
<% @g = @f * 100 %>
<% @h = [@g, 100].min %>
<% @i = Percentage.find_by_user_id(@user) %>
<% @i.update_attribute(x.to_sym, @h) %>
x <%= @i.send(x) %>%
<% @x = @i.send(x) %>

x%

ty!我错过了发送(x)ty!我错过了发送(x)你在做这一切吗?所有这些逻辑都将更愉快地存在于模型(名称?)中,这是您正在迭代的一个集合。还有,@user是如何被填充的?你是在一个视图中完成这一切的?所有这些逻辑都将更愉快地存在于模型(名称?)中,这是您正在迭代的一个集合。另外,@user是如何填充的?