0

I am creating a week-by-week report of meetings using Ruby. I have a large array that contains the names of the meetings, and I have multiple instances of each element based on how many weeks they occured.

Simplified example I am starting with:

meetingsArray = ["boardInvestorsMeeting", "clientMeeting", "clientMeeting", "clientMeeting", "waterCoolerChat", "waterCoolerChat",  "waterCoolerChat", "waterCoolerChat"]

My attempt:

meetingsArray = ["boardInvestorsMeeting", "clientMeeting", "clientMeeting", "clientMeeting", "waterCoolerChat", "waterCoolerChat",  "waterCoolerChat", "waterCoolerChat"]
for m in meetingsArray[m.to_i]
  if (meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 1] && meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 2] && meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 3])
    puts "Week of the 1st: " + "#{meetingsArray[m.to_i].inject{|a| a}}"
  elsif (meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 1] && meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 2])
    puts "Week of the 8th: " + "#{meetingsArray[m.to_i].inject{|a| a}}"
  elsif (meetingsArray[m.to_i] === meetingsArray[(m.to_i) + 1])
    puts "Week of the 15th: " + "#{meetingsArray[m.to_i].inject{|a| a}}"
  else
    puts "Week of the 22nd: " + "#{meetingsArray[m.to_i].inject{|a| a}}"
  end
end

Console error:

iMac:workspace user1$ ruby loop.rb
loop.rb:3:in `<main>': undefined method `each' for "boardInvestorsMeeting":String (NoMethodError)

Desired results:

Week of the 1st: boardInvestorsMeeting
Week of the 1st: clientMeeting
Week of the 8th: clientMeeting
Week of the 15th: clientMeeting
Week of the 1st: waterCoolerChat
Week of the 8th: waterCoolerChat
Week of the 15th: waterCoolerChat
Week of the 22nd: waterCoolerChat

Note that "clientMeeting" has "Week of the 1st:" then "Week of the 8th:" then "Week of the 15th:" in front of it because is repeated twice; while "boardInvestorsMeeting" only has "Week of the 1st:" in front of it because it isn't repeated and only appears in meetingsArray once.

9
  • 1
    It is not clear which part is what you start with, which part is your expected result, and which part is your attempt. Commented Feb 25, 2016 at 17:30
  • @undur_gongor I made the edit. Thanks! Commented Feb 25, 2016 at 17:35
  • @sawa I just restructured my question based on your recommendations. Commented Feb 25, 2016 at 17:49
  • Your code doesn't run at all. Can you edit your question to include the actual code you're using? It fails on the first line, because you're not using for correctly. Please edit your question to include the code that produces the results you say you're getting. Commented Feb 25, 2016 at 18:50
  • @Jordan I edited the question to reflect the code I am getting. I can't see how I am using the for loop incorrectly and when I replace the if statement inside to just puts the meetingsArray I get no errors. Commented Feb 25, 2016 at 19:13

1 Answer 1

1

Here you go!

meetingsArray = ["boardInvestorsMeeting", "clientMeeting", "clientMeeting", "clientMeeting", "waterCoolerChat", "waterCoolerChat",  "waterCoolerChat", "waterCoolerChat"]
counts = Hash.new(0)
meetingsArray.each { |name| counts[name] += 1 }

counts.each do |k,v|
    case v
    when 1
        puts "Week of the 1st: #{k}" 
    when 2
        puts "Week of the 1st: #{k}"
        puts "Week of the 8th: #{k}"
    when 3
        puts "Week of the 1st: #{k}"
        puts "Week of the 8th: #{k}"
        puts "Week of the 15th: #{k}"
    when 4
        puts "Week of the 1st: #{k}"
        puts "Week of the 8th: #{k}"
        puts "Week of the 15th: #{k}"
        puts "Week of the 22nd: #{k}"
    end
end
Sign up to request clarification or add additional context in comments.

1 Comment

Clean and easy way to attack my problem. Thank you.

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.