ruby - How to remove a trailing comma? -


given strings like:

bob bob, bob bob burns, 

how can return w/o comma?

bob bob bob bob burns 

also, want method not break if passed nil, return nil?

def remove_trailing_comma(str)   !str.nil? ? str.replace(",") :nil end 

my thought use string.chomp:

returns new string given record separator removed end of str (if present).

does want?

def remove_trailing_comma(str)     str.nil? ? nil : str.chomp(",") end 

Comments

Popular posts from this blog

How can I edit my VIM config so that Vim treats ".ejs" files the same as it currently treats my html files? -

Python __call__ special method practical example -