Ruby - MethodsWhy 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.Check Answer
Step-by-Step SolutionSolution:Step 1: Understand purpose of bang and non-bang methodsBang methods usually modify the object directly (destructive), while non-bang methods return a new object (non-destructive).Step 2: Reason why both versions existHaving both versions gives programmers choice between changing original data or keeping it unchanged for safety.Final Answer:To provide both safe (non-destructive) and destructive versions for flexibility. -> Option DQuick 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 deprecatedAssuming bang means error-raisingBelieving bang methods only work on strings
Master "Methods" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Arrays - Array modification (push, pop, shift, unshift) - Quiz 2easy Hashes - Symbol keys vs string keys decision - Quiz 5medium Hashes - Hash as named parameters pattern - Quiz 13medium Loops and Iteration - Loop method for infinite loops - Quiz 13medium Operators and Expressions - Why operators are methods in Ruby - Quiz 2easy Operators and Expressions - Arithmetic operators - Quiz 15hard Ruby Basics and Runtime - Comments and documentation - Quiz 12easy String Operations - String methods (upcase, downcase, strip) - Quiz 7medium String Operations - String methods (upcase, downcase, strip) - Quiz 9hard Variables and Data Types - Type checking with .class and .is_a? - Quiz 5medium