What is the best way to convert a Ruby string range to a Range object

后端 未结 8 2184
一生所求
一生所求 2020-12-29 23:38

I have some Ruby code which takes dates on the command line in the format:

-d 20080101,20080201..20080229,20080301

I want to run for all da

8条回答
  •  温柔的废话
    2020-12-29 23:57

    Combining @Purfideas answer with another answer somewhere on StackOverflow, I solved this by also surrounding the code with an input check, so the only thing used is a valid enumerable

    if !value[/^[0-9]+\.\.[0-9]+$/].nil?
        ends = value.split('..').map{|d| Integer(d)}
        value = ends[0]..ends[1]
    end
    

    It essentially rewrites your string value to a enumerable value. This comes in handy if you add a enumerable field in a yaml config file.

    If you need it for your application, you could extend the regex with an optional third literal dot, that could be optional.

提交回复
热议问题