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 =开发者_Python百科 [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
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.
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.
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]
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 %>
精彩评论