Skip to content

Instantly share code, notes, and snippets.

@adellhk
Created August 30, 2015 05:00
Show Gist options
  • Save adellhk/79c42c429613bfca481b to your computer and use it in GitHub Desktop.
Save adellhk/79c42c429613bfca481b to your computer and use it in GitHub Desktop.
def separate_comma(num)
separated_num = num.to_s.split("").reverse
result = []
separated_num.each_with_index.map do |num, index|
if index % 3 == 0 && index != 0
result << num + ","
else
result << num
end
end
result.reverse.join("")
end
// can ommit the need to declare result if you use each_with_index.map, but I think this form is simpler.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment