Bird
0
0

Why do some Ruby methods have both bang and non-bang versions, like gsub and gsub!?

hard📝 Conceptual Q10 of 15
Ruby - Methods
Why do some Ruby methods have both bang and non-bang versions, like gsub and gsub!?
ATo mark methods that only work on strings.
BBecause bang methods are deprecated versions.
CTo indicate methods that always raise exceptions.
DTo provide both safe (non-destructive) and destructive versions for flexibility.
Step-by-Step Solution
Solution:
  1. Step 1: Understand purpose of bang and non-bang methods

    Bang methods usually modify the object directly (destructive), while non-bang methods return a new object (non-destructive).
  2. Step 2: Reason why both versions exist

    Having both versions gives programmers choice between changing original data or keeping it unchanged for safety.
  3. Final Answer:

    To provide both safe (non-destructive) and destructive versions for flexibility. -> Option D
  4. Quick Check:

    Bang vs non-bang = destructive vs non-destructive [OK]
Quick Trick: Bang methods = destructive; non-bang = safe copy [OK]
Common Mistakes:
  • Thinking bang methods are deprecated
  • Assuming bang means error-raising
  • Believing bang methods only work on strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes