A More elegant way of doing this?

I keep saying to myself there must be a better way but I can't see it right now.. ideas?

i = 0; lose = 0; win = 0
while i < @array.size
  results = @array[i].results
  q = 0
  while q < results.size
    if results[q].to_i == 0 then
      lose += 1
    elsif results[q].to_i == 1 then
      win += 1
    else
      puts results[q]
      puts "false"
    end
    q += 1
  end
  i+=1
end
if win == lose then
  puts "true"
else
  puts "false"
end

Answers


You can use array.each instead of while loops.

You can use array.count instead of manually inspecting each array:

lose = results.count { |r| r.to_i == 0 }
win = results.count { |r| r.to_i == 1 }

# or possibly if the array can only contain wins and losses
win = results.count - lose

Need Your Help

App bundle with one main executable and one helper app

objective-c xcode osx cocoa

I wrote an app which uses an external ask pass(a small app I wrote for the gui tool) cmd line tool for accessing ssh. Everything works fine so far in development mode. But when I want to archive/bu...

Import Error using cPickle in Python

python python-2.7 pickle

I am using Pickle in Python2.7. I am getting error while using cPickle.load() method. The code and error is shown below. Can someone guide me through this?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.