如何在 Rails 上循环使用整个数组 ruby

发布于 2024-09-10 09:36:17 字数 880 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手长情犹 2024-09-17 09:36:17

我相信cycle()的工作方式,它将输出转换为字符串。因此,当您调用 @tempt_array[0] 时,它会返回类似 "1234567"[0]

我在控制台中的测试返回了我所期望的内容:

"1234567".to_s[0]
=> 49
"1234567".to_s[1]
=> 50
"1234567".to_s[2]
=> 51

我相信这些是 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:

"1234567".to_s[0]
=> 49
"1234567".to_s[1]
=> 50
"1234567".to_s[2]
=> 51

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.

So要识趣 2024-09-17 09:36:17

我承认我什至不确定您的代码现在是如何工作的,因为 cycle 是一个 Enumerable 方法,并且您似乎没有在任何可枚举对象上调用它。

无论如何,要创建一个将在两个数组之间永远循环的枚举器,您可以这样做:

(array1 + array2).cycle

例如:

array1 = 1.upto(7).to_a
array2 = 7.downto(1).to_a
sequence = (array1 + array2).cycle
sequence.take 49 
# => [1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7]

编辑:卡尔的解释提供了缺失的部分。我正在考虑 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:

(array1 + array2).cycle

So for example:

array1 = 1.upto(7).to_a
array2 = 7.downto(1).to_a
sequence = (array1 + array2).cycle
sequence.take 49 
# => [1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7]

Edit: Karl's explanation provided the missing piece. I was thinking of Enumerable#cycle, but this is TextHelpers#cycle. I think the Enumerable method is actually closer to what you were looking for, though.

枉心 2024-09-17 09:36:17

您也可以完全切断循环并仅使用

@temp_array = @good_array.zip(@bad_array).flatten

[1,2,3].zip([3,4,5]).flatten #  => [1, 3, 2, 4, 3, 5]

You could also cut out the cycle completely and just use

@temp_array = @good_array.zip(@bad_array).flatten

[1,2,3].zip([3,4,5]).flatten #  => [1, 3, 2, 4, 3, 5]
遗忘曾经 2024-09-17 09:36:17

这就是我认为你想要做的事情,尽管我不是 100% 确定:

a1 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
a2 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]

a1.zip(a2).each_with_index{|array, index| array.reverse! if index % 2 == 0}.map{|a| a.first}
# => ["a", "B", "c", "D", "e", "F", "g", "H", "i", "J"]

# Alternatively:
a1.zip(a2).each_with_index{|array, index| array.reverse! unless index % 2 == 0}.map{|a| a.first}
# => ["A", "b", "C", "d", "E", "f", "G", "h", "I", "j"]

我确信有一种更短的方法可以做到这一点,但我现在想不到。

编辑:要回答您在评论中的问题 - 都 <% %> <%=%>将运行它们包含的 ruby​​ 代码,但第二个将输出对 HTML 的响应,而第一个则不会。因此,条件语句通常放在最前面。因此,例如,您可以这样做:

<% if @user.admin? %>
  <%= edit_link %>
<% else %>
  You're not an admin!
<% end %>

Here's what I think you're trying to do, though I'm not 100% certain:

a1 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
a2 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]

a1.zip(a2).each_with_index{|array, index| array.reverse! if index % 2 == 0}.map{|a| a.first}
# => ["a", "B", "c", "D", "e", "F", "g", "H", "i", "J"]

# Alternatively:
a1.zip(a2).each_with_index{|array, index| array.reverse! unless index % 2 == 0}.map{|a| a.first}
# => ["A", "b", "C", "d", "E", "f", "G", "h", "I", "j"]

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:

<% if @user.admin? %>
  <%= edit_link %>
<% else %>
  You're not an admin!
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文