如何在 Rails 上循环使用整个数组 ruby
我对 ruby on Rails 和 Web 开发还是新手,所以请耐心等待:
我有两个数组 a1 = [1,2,3,4] b1 = [7,6,5,4]
我想替换我正在使用的数组;在 a1[] 和 b1[] 之间切换。
我目前正在尝试使用 Cycle() 命令来完成此任务。
<% @good_bad = [7,6,5,4,3,2,1] %>
<% @bad_good = [1,2,3,4,5,6,7] %>
WITHOUT CYCLE:</br>
<% @super = @bad_good%>
<%= @super%>
<%= @super[0]%>
<%= @super[1]%>
<%= @super[2]%>
WITH CYCLE: </br>
<% @temp_array = cycle(@bad_good , @good_bad , :name => "rating")%>
<%= @temp_array%>
<%= @temp_array[0]%>
<%= @temp_array[1]%>
<%= @temp_array[2]%>
这将显示:
ITHOUT CYCLE: 1234567 1 2 3 WITH CYCLE: 1234567 49 50 51
我希望打印输出与第一个周期相同,它将@temp存储到@bad_good。
我可能缺少一些基本的东西。奇怪的是,当我尝试获取数组的单个值时,它打印出 49,50,51,但是当我打印出整个数组时,它是准确的?
任何建议表示赞赏, 谢谢, D
I'm still new to ruby on rails and web development so please bear with me:
I have two arrays
a1 = [1,2,3,4]
b1 = [7,6,5,4]
I want to alternate which array i'm using; switching between a1[] and b1[].
I'm currently trying to use the cycle() command to accomplish this.
<% @good_bad = [7,6,5,4,3,2,1] %>
<% @bad_good = [1,2,3,4,5,6,7] %>
WITHOUT CYCLE:</br>
<% @super = @bad_good%>
<%= @super%>
<%= @super[0]%>
<%= @super[1]%>
<%= @super[2]%>
WITH CYCLE: </br>
<% @temp_array = cycle(@bad_good , @good_bad , :name => "rating")%>
<%= @temp_array%>
<%= @temp_array[0]%>
<%= @temp_array[1]%>
<%= @temp_array[2]%>
This will display:
ITHOUT CYCLE: 1234567 1 2 3 WITH CYCLE: 1234567 49 50 51
I would expect the print out to be the same since the first cycle it is storing the @temp to @bad_good.
There is probably something basic i'm missing. It's weird how when i try to get the single values of the array it print out 49,50,51, but when i print out the whole array it is accurate?
Any advice appreciated,
Thanks,
D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信cycle()的工作方式,它将输出转换为字符串。因此,当您调用
@tempt_array[0]
时,它会返回类似"1234567"[0]
我在控制台中的测试返回了我所期望的内容:
我相信这些是 ASCII “1”、“2”和“3”的字符代码如下所示:http://www.asciitable。 com/
您可能需要编写自己的枚举器方法,或者找到一个不同的方法来使用。
I believe the way cycle() works, it will convert the output to a string. So when you call
@tempt_array[0]
, it returns something like"1234567"[0]
My tests in the console returned what I expected:
I believe these are the ASCII character codes for "1", "2", and "3" as seen here: http://www.asciitable.com/
You'll probably need to write your own enumerator method, or find a different one to use.
我承认我什至不确定您的代码现在是如何工作的,因为
cycle
是一个 Enumerable 方法,并且您似乎没有在任何可枚举对象上调用它。无论如何,要创建一个将在两个数组之间永远循环的枚举器,您可以这样做:
例如:
编辑:卡尔的解释提供了缺失的部分。我正在考虑
Enumerable#cycle
,但这是TextHelpers#cycle
。不过,我认为 Enumerable 方法实际上更接近您正在寻找的方法。I will admit that I'm not even sure how your code is working now, because
cycle
is an Enumerable method and you don't appear to be calling it on any enumerable object.At any rate, to create a Enumerator that will cycle between two arrays forever, you'd do it like this:
So for example:
Edit: Karl's explanation provided the missing piece. I was thinking of
Enumerable#cycle
, but this isTextHelpers#cycle
. I think the Enumerable method is actually closer to what you were looking for, though.您也可以完全切断循环并仅使用
You could also cut out the cycle completely and just use
这就是我认为你想要做的事情,尽管我不是 100% 确定:
我确信有一种更短的方法可以做到这一点,但我现在想不到。
编辑:要回答您在评论中的问题 - 都 <% %> <%=%>将运行它们包含的 ruby 代码,但第二个将输出对 HTML 的响应,而第一个则不会。因此,条件语句通常放在最前面。因此,例如,您可以这样做:
Here's what I think you're trying to do, though I'm not 100% certain:
I'm sure there's a shorter way to do it, but I can't think of it right now.
Edit: To answer your questions in the comment - both <% %> and <%= %> will run the ruby code they contain, but the second will output the response to the HTML, while the first one won't. Therefore, conditional statements are usually put in the first. So, for example, you could do: