0

EDITED QUESTION

I have the following method:

def stats_by_day(league)
 league.days_of_league.collect do |day|
  roster_for(league).collect do |celeb|
    celeb.tweets_this_day(day).inject(0) {|sum, n| sum + n.retweet_count + n.favorite_count}
  end
 end
end

This gives me:

Day1, Day2, Day3  Day1  Day2  Day3
 [345, 647, 567], [344, 222, 675] etc
      TEAM1            TEAM2              

However what I want is

Team1 Team2 Team3   Team1 Team2 Team3
 [345, 344, 567]     [647, 222, 876]
       DAY1                DAY2

I've tried switching the 2nd and 3rd lines of the method but that didn't work. Do I have what I need to get my desired output?

OLD QUESTION (for reference)

If I have...

[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3]]

how would I get...

[[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]

Looking for a ruby approach. Thanks!

1
  • 1
    Do you mean for the elements a1, b1,... to be methods or local variables? If so, your output must contain the values of those methods or variables. For example, if a1=1, b1=2, a2=3, b2=4, where a=[[a1,b1],[a2,b2]] => [[1,2],[3,4]], your desired result be [[1,3][2,4]]. If you meant to have [[:a1, :a2... or [['a1', 'a2'..., please edit both arrays. Commented Apr 16, 2014 at 19:49

2 Answers 2

4

If the nested arrays are all of the same size you can use the Array#transpose method:

ary = [[:a1, :b1, :c1],[:a2, :b2, :c2],[:a3, :b3, :c3]]
ary.transpose
# => [[:a1, :a2, :a3], [:b1, :b2, :b3], [:c1, :c2, :c3]]
Sign up to request clarification or add additional context in comments.

Comments

1

You can do

 a.flatten.group_by { |x| x[/[a-zA-Z]+/] }.values
 => [["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]] 

3 Comments

Several problems here, I'm afraid. Firstly, nowhere is it said that the elements of the array are strings or symbols. (Yes, @toro2k used symbols in his example, but transpose doesn't care what the objects are.) Presumably, a1 is a method or local variable. Suppose we had a = [[a1,b1],[a2,b2]] where a1 = 1, b1 = 2, a2 = 3, b2 = 4. Then a => [[1, 2], [3, 4]], causing your regex to choke. Even if a contained strings or symbols, your code depends on the data. Suppose, for example, a = [[:a1,:a2],[:a3,:a4]]. Your code incorrectly returns [:a1, :a2, :a3, :a4].
Thanks for inputs. Yes the example I have given is dependent on the data. I do not think there will be case where a1 will be a method or a variable, because operations happen on data but not on method definitions itself or by variables itself. However, agreed that using strings is not the best bet here.
One commonly defines array whose elements are the values of variables or methods; e.g., a = [a1,a2], but of course Ruby evaluates those values when creating the array a; e.g, a = [1,2]. The values of a1 and a2 can be any objects; there is no reason to assume they will be strings rather than hashes, for example. When you see code that looks like [a1,a2], what can a1 and a2 be other than methods or local variables? If the array were instead defined with literals, you'd see something like [1,:a,'b'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.