hoodwink.d enhanced

Wed Oct 4

A coercible string

class CoercibleString < String class << self def coerce(string) new(string).coerce end end def coerce attempt = nil while coercions.next? attempt = coercions.next break if !attempt.nil? end attempt.nil? ? self : attempt end private def coercions @coercions ||= Generator.new do |@generator| try { self == 'true' } try { [self == 'false', false] } try { Integer(self) } try { [Date.parse(self), Time.parse(self)] } end end def try attempt, desired = yield if attempt @generator.yield(desired.nil? ? attempt : desired) end rescue ArgumentError @generator.yield nil end end CoercibleString.coerce('25') # => 25 CoercibleString.coerce('9/1/81') # => Tue Sep 01 00:00:00 CDT 1981 CoercibleString.coerce('false') # => false CoercibleString.coerce('Hello!') # => "Hello!"
When your skillet is really really hot, drop the meat into it. Once it hits, it’s going to sizzle an awful lot. You’ll be scared. You’ll want to move the meat around. Don’t touch it.

Chris McAvoy on How to Cook a Steak