19 lines
353 B
Ruby
19 lines
353 B
Ruby
|
class YieldParser
|
||
|
|
||
|
Result = Struct.new(:number, :label)
|
||
|
|
||
|
def self.parse(yield_string)
|
||
|
|
||
|
match = /(\d+(?:\.\d*)?)\s*(\w[\w\s]*)?/.match(yield_string)
|
||
|
|
||
|
case
|
||
|
when match.nil?
|
||
|
nil
|
||
|
when match[2]
|
||
|
Result.new(match[1].to_f, match[2].strip.singularize)
|
||
|
else
|
||
|
Result.new(match[1].to_f, 'each')
|
||
|
end
|
||
|
end
|
||
|
|
||
|
end
|