Skip to content

Instantly share code, notes, and snippets.

View JEG2's full-sized avatar

James Edward Gray II JEG2

View GitHub Profile
class Context
def initialize(variables)
variables.each do |name, value|
instance_variable_set("@#{name}", value)
end
end
def run(code, start_line)
context = init_context
load_variables(context)
@JEG2
JEG2 / gist:570556
Created September 8, 2010 18:22 — forked from pragdave/gist:570434
Trying to remove some noise, but I don't think I helped.
input = [ 1, 2, 3, 4, 5, 8, 9, 11, 12, 13, 15 ]
# divide the input into runs of consecutive numbers
last = input.first
s = input.slice_before(lambda { |i| [i != last + 1, (last = i)].first })
# replace runs of 3 or more with first-last
p s.map {|runs| runs.size < 3 ? runs : "#{runs.first}-#{runs.last}"}
.flatten
.join(', ') # => 1-5, 8, 9, 11-13, 15