views:

49

answers:

4

Hi,

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

+2  A: 

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.

Chuck
Truthfully, I didn't even know that the Enumerable#cycle existed. I'll read up more on it. Thanks for the advise thou.Basically, I have 10 rows of radio buttons. 1st row should represent values from 1 to 7. Second row represents values from 7 to 1. And this alternates for each row.I guess the easier way was just to mod the index to see if its an even or odd row and then choose which array to grab the value from. I just didn't know how conditions worked with ruby on rails or maybe i'm just a terrible googler =(. Thanks THOU!
+3  A: 

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.

Karl
That make sense....I was wondering where these numbers came from.Thanks!!
+1  A: 

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]
Paul Rubel
Oh, this may be what he has in mind. I thought he meant to append the arrays end to end, but interspersing this way might be right.
Chuck
I wanted to avoid building a super long array of the values. Since every time I add an addition row of radio buttons, it'll result in me appending more arrays together.Thanks !!
A: 

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 %>
PreciousBodilyFluids
Actually you gave me the easiest solution that i need to try. I just didn't really know how condition statements worked in ruby on rails for view pages. I'm a C++ programmer and really new to web development. I could just see if the index % 2 == 0 to determine if its old or even, and choose which array to grab the value from based on that.Just wanted to clarify, <% %> represent ruby code. So anything in those brackets can be ruby code? Sorry again if thats a stupid question. THANKS!!