Skip to content

Instantly share code, notes, and snippets.

@passalini
Created November 13, 2014 18:19
Show Gist options
  • Save passalini/134a79d4df617f855fe5 to your computer and use it in GitHub Desktop.
Save passalini/134a79d4df617f855fe5 to your computer and use it in GitHub Desktop.
Using gsub with strings and hash
def changing_ending_char(string)
ending_chars = {
'!' => :_dangerous,
'?' => :_question
}
string.gsub(/(?<char>[\?|\!])/, ending_chars)
end
changing_ending_char('asd') # => 'asd'
changing_ending_char('asd!') # => 'asd_dangerous'
changing_ending_char('asd?') # => 'asd_question'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment