Solution to Max Range Sum challenge on CodeEval
Just for clarity, I'd split the code into a couple of methods. One to parse input, and one to find the best yield.

To tackle the latter first: Have a look at Enumerable#each_cons, and Enumerable#reduce (aka inject).

Given an array of gains/losses (values) and the length to examine (days), you can find the "best streak" like so:

values.each_cons(days).map { |streak| streak.reduce(&amp;:+) }.max


In English: Take each consecutive "streak" of days values; map each of these streaks to its sum; take the maximum of these sums. As an example:

days = 3
values = [1, 2, -1, 3, 4]

streaks = values.each_cons(days).to_a              # =&gt; [ [1, 2, -1], [2, -1, 3], [-1, 3, 4] ]
sums = streaks.map { |streak| streak.reduce(&amp;:+) } # =&gt; [2, 4, 6]
max = sums.max                                     # =&gt; 6


There are two other things to handle, to solve the task: Return zero if the best gain is actually a loss, or if there are not enough values to satisfy days.

In all, you might do something like:

def best_streak(days, values)
  return 0 if values.count &lt; days
  gain = values.each_cons(days).map { |streak| streak.reduce(&amp;:+) }.max
  gain &gt; 0 ? gain : 0
end


As for parsing the input, your current code is ok, though I'd still handle it separately.

def parse_line(line)
  days, *values = line.strip.split(/[;\s]/)
  [days.to_i, values.map(&amp;:to_i)]
end


This basically just splits the line in to its parts, treating the first number as the number of days, and the rest as the values.

To put it all together, you can do something like:

File.open(ARGV[0]).each do |line|
  days, values = parse_line(line)
  puts best_streak(days, values)
end


However, it seem that in the CodeEval context, input is passed on STDIN; not in a file. So really it should probably be:

STDIN.each_line do |line|
  days, values = parse_line(line)
  puts best_streak(days, values)
end




There may be a more efficient way to do all this, but this is probably the most readable.