Skip to content

Instantly share code, notes, and snippets.

@abhinaykumar
Last active March 12, 2016 06:56
Show Gist options
  • Save abhinaykumar/07af4272df2d497d8875 to your computer and use it in GitHub Desktop.
Save abhinaykumar/07af4272df2d497d8875 to your computer and use it in GitHub Desktop.
challenge is to add ',' at the end of every number which is not pin-code. Pincodes are the number lesser or equal to 5.
# challenge is to add ',' at the end of every number which is not pin-code. Pincodes are the number lesser or equal to 5.
# Also, make sure they all are one space apart.
# ex: "123 23424324 34534 2342442234"
# function should return: "123 23424324, 34534 2342442234,"
#
a = "1234 23434242 232342442 345 2342341234"
a.scan(/[\w']+/) do |w|
if w.length > 5
b << "#{w},"
next
end
b << w
end
=> "1234 23434242 232342442 345 2342341234"
b
=> ["1234", "23434242,", "232342442,", "345", "2342341234,"]
b.join(' ')
=> "1234 23434242, 232342442, 345 2342341234,"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment