Ruby - String Operations
Which Ruby code snippet correctly replaces only the first occurrence of 'bird' with 'fish' in the string
"bird bird bird" using sub?"bird bird bird" using sub?sub behaviorsub replaces only the first occurrence of the pattern.sub directly on the string to replace the first 'bird' with 'fish'. "bird bird bird".gsub('bird', 'fish') uses gsub which replaces all occurrences. Options C and D combine sub and gsub unnecessarily.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions