Bird
0
0

Which of the following is a valid method name in Ruby?

easy📝 Conceptual Q2 of 15
Ruby - Methods
Which of the following is a valid method name in Ruby?
A2times
Btimes 2
Ctimes2
Dtimes-2
Step-by-Step Solution
Solution:
  1. Step 1: Check Ruby method naming rules

    Method names must start with a letter or underscore and can include letters, numbers, and underscores.
  2. Step 2: Evaluate each option

    2times starts with a number (invalid). times2 starts with a letter and includes a number (valid). times-2 contains a hyphen (invalid). times 2 contains a space (invalid).
  3. Final Answer:

    times2 -> Option C
  4. Quick Check:

    Method names start with letter or underscore [OK]
Quick Trick: Method names cannot start with numbers or contain spaces [OK]
Common Mistakes:
  • Starting method name with a number
  • Using spaces in method names
  • Using special characters like hyphens

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes