Use the method String#sub which replaces the first occurrence of a string or pattern:
"meu nome é neymar,".sub(/,\z/, '')
=> "meu nome é neymar"
"substitui so a , do final,".sub(/,\z/, '')
=> "substitui so a , do final"
\z
is the default for the end of the string, so /,\z/
is the default for a comma followed by the end of the string. .sub(/,\z/, '')
, so replace a comma at the end of the string with "nothing" - that is, remove the comma at the end of the line, but not the commas that are in other parts of the text.
Enter comments. Explain WHY this code answers the question and what is the return of it. What the
sub()
is doing with the data?– Andrei Coelho