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

后端 未结 8 2207
一生所求
一生所求 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:49

    Here suppose you want to store the hash as a system constant value and fetch it in any model. The hash key will be a range value.

    hash_1 = {1..5 => 'a', 6..12 => 'b', 13..67 => 'c', 68..9999999 => 'd'}
    

    Then create the system constant with value as hash_1.to_json. .to_json will convert your hash object to JSON object. Now inside the code create a new hash hash_2,

    JSON.parse(SystemConstant.get('Constant_name')).each{|key,val| temp_k=key.split('..').map{|d| Integer(d)}; hash_2[temp_k[0]..temp_k[1]] = val}
    

    The new hash_2 will be the required hash_1

提交回复
热议问题