ruby - 'string' to ['s', 'st', 'str', 'stri', 'strin', 'string'] -
what's elegant way of doing
'string' => ['s', 'st', 'str', 'stri', 'strin', 'string'] i've been trying think of 1 liner, can't quite there.
solutions welcome, thanks.
you can use abbrev in standard library
require 'abbrev' s = abbrev::abbrev(['string']).keys puts s.inspect i must add other way use after requiring abbrev library, .abbrev method added array:
require 'abbrev' s = ['string'].abbrev.keys puts s.inspect if order important match return in question, call .sort on keys.
Comments
Post a Comment