Ruby - File IOWhich of the following is the correct Ruby syntax to open a file named log.txt for appending?AFile.open('log.txt', 'a')BFile.open('log.txt', 'r')CFile.open('log.txt', 'w')DFile.open('log.txt', 'rw')Check Answer
Step-by-Step SolutionSolution:Step 1: Identify append mode syntaxIn Ruby, 'a' mode opens a file for appending, adding data at the end.Step 2: Check other options'r' is read-only, 'w' is write (overwrite), 'rw' is invalid mode string.Final Answer:File.open('log.txt', 'a') -> Option AQuick Check:Append mode syntax = 'a' = C [OK]Quick Trick: Use 'a' to add data at file end [OK]Common Mistakes:Using 'rw' which is invalidConfusing 'w' with appendUsing 'r' when writing is needed
Master "File IO" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Blocks, Procs, and Lambdas - Method objects with method() - Quiz 4medium Blocks, Procs, and Lambdas - Closures and variable binding - Quiz 14medium Blocks, Procs, and Lambdas - Block given? check - Quiz 14medium Class Methods and Variables - Open classes (reopening classes) - Quiz 15hard Classes and Objects - Initialize method as constructor - Quiz 4medium Enumerable and Collection Processing - Group_by for categorization - Quiz 13medium Error Handling - Ensure for cleanup - Quiz 3easy Error Handling - Why error handling uses rescue - Quiz 1easy Error Handling - Rescue modifier (inline form) - Quiz 3easy File IO - Why file handling matters - Quiz 13medium