Skip to content

Instantly share code, notes, and snippets.

@thatrubylove
Created April 17, 2014 21:59
Show Gist options
  • Save thatrubylove/11013924 to your computer and use it in GitHub Desktop.
Save thatrubylove/11013924 to your computer and use it in GitHub Desktop.
a-deeper-look-at-rubys-enumerable-example-2
class Roll < Array
def small_straight?
sort.each_cons(4).any? {|vals| in_series?(vals) }
end
def large_straight?
sort.each_cons(5).all? {|vals| in_series?(vals) }
end
private
def in_series?(vals)
vals == (vals.first..vals.last).to_a
end
end
Roll.new([1,3,4,5,6]).small_straight? #=> true
Roll.new([1,3,4,5,6]).large_straight? #=> false
Roll.new([2,3,4,5,6]).large_straight? #=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment