How to format strings in an array in ruby?

array = [
[0] "Bonk  Radek  S  Male  Green  6/3/1978",
[1] "Bouillon  Francis  G  Male  Blue  6/3/1975",
[2] "Smith  Steve  D  Male  Red  3/3/1985"
]

at the moment each string is formatted:

Last Name, First Name, Middle, Gender, Fav Color, Birth Date

I need to write code that will format it:

last name, first name, middle, gender, date of birth, favorite color

How do I rearrange the strings to be in the format I want and return the array?

------------------------------------------------------------------------------
array = [
  "Bonk  Radek  S  Male  Green  6/3/1978",
  "Bouillon  Francis  G  Male  Blue  6/3/1975",
  "Smith  Steve  D  Male  Red  3/3/1985"
]
array.map { |s| s.sub(/(\S+)(\s+)(\S+)$/, '\3\2\1') }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章