lundi 1 octobre 2018

Default value in gsub Ruby on rails

I have an array that looks like this

[
  "---\n",
  ":date: 2018-07-31\n  :story_points: 4.0\n  :remaining_hours: 4.0\n ",
  ":date: 2018-08-01\n  :story_points:    \n  :remaining_hours: 4.0\n ",
  ":date: 2018-08-22\n  :story_points: 8.0\n  :remaining_hours: 0.0\n "
]

I want the date and story_points in 2 different arrays. The output should look like below:

["2018-07-31", "2018-08-01", "2018-08-22"]

["4.0", "0.0", "8.0"]

If the value for story_points is not there then it should use default value of 0.0

I have tried till below as suggested in my earlier post by a gentlemen

arr.join.gsub(/(?<=:date: )\d{4}-\d{2}-\d{2}/).to_a

arr.join.gsub(/(?<=:story_points: )\d{1}.\d{1}/).to_a 

The above will fetch me

["2018-07-31", "2018-08-01", "2018-08-22"]

["4.0", "8.0"]

I am not able to fetch the default values. Can anyone help on this?

Aucun commentaire:

Enregistrer un commentaire