Skip to content

Instantly share code, notes, and snippets.

@nthj
Created February 13, 2012 13:12
Show Gist options
  • Save nthj/1816922 to your computer and use it in GitHub Desktop.
Save nthj/1816922 to your computer and use it in GitHub Desktop.
Other
class Estimated < Other; end
class Forwarded < Other; end
case discussable
when Estimated then :queued # discussable.estimate?
when Forwarded then :read # discussable.forward?
else :unread
end
# Create by passing the method you want to call
# ESTIMATED = Other.new(:estimate?)
#
# Guesses by class name if no flag passed:
# class Estimated < Other; end
class Other < Struct.new(:flag)
class << self
def === other
# try estimated? estimate? estimat?, forwarded? forwarde? forward?
other.send (0..2).map { |o| name.demodulize.downcase[0..(-o)].questionable }
end
end
def === other
other.try flag
end
end
# String extensions
class String
def questionable
questionable? ? to_s : "#{to_s}?"
end
def questionable!
replace questionable
end
def questionable?
self[-1, 1] == '?'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment