Ruby - Basics and RuntimeWhich of the following is the correct way to define a method in Ruby that returns a greeting?Adef greet(name)\n 'Hello, ' + name + '!'\nendBfunction greet(name) { return 'Hello, ' + name + '!'; }Cdef greet(name):\n return 'Hello, ' + name + '!'\nendDdef greet(name) { 'Hello, ' + name + '!' } endCheck Answer
Step-by-Step SolutionSolution:Step 1: Recall Ruby method syntaxRuby methods start with def, followed by the method name and parameters, and end with end.Step 2: Check each option's syntaxdef greet(name)\n 'Hello, ' + name + '!'\nend uses correct Ruby syntax; B is JavaScript style; C uses Python-like colon; D uses braces incorrectly.Final Answer:def greet(name)\n 'Hello, ' + name + '!'\nend -> Option AQuick Check:Ruby method syntax = def ... end [OK]Quick Trick: Ruby methods use def and end keywords [OK]Common Mistakes:MISTAKESUsing JavaScript or Python syntax in RubyForgetting the end keywordUsing braces instead of end
Master "Basics and Runtime" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Control Flow - Unless for negated conditions - Quiz 4medium Control Flow - Case/when statement - Quiz 10hard Control Flow - Guard clauses pattern - Quiz 9hard Hashes - Why hashes are used everywhere in Ruby - Quiz 3easy Methods - Variable-length arguments (*args) - Quiz 8hard Methods - Bang methods (ending with !) - Quiz 14medium Operators and Expressions - Spaceship operator (<=>) - Quiz 9hard String Operations - String slicing and indexing - Quiz 10hard String Operations - String methods (upcase, downcase, strip) - Quiz 14medium String Operations - Heredoc syntax for multiline strings - Quiz 4medium