Bird
0
0

Given a list of strings, how can you convert them to symbols efficiently in Ruby?

hard📝 Application Q8 of 15
Ruby - Variables and Data Types

Given a list of strings, how can you convert them to symbols efficiently in Ruby?

names = ["alice", "bob", "carol"]
symbols = ?
Anames.map { |name| :name }
Bnames.map { |name| name.to_sym }
Cnames.map(&:to_s)
Dnames.map { |name| name.to_s }
Step-by-Step Solution
Solution:
  1. Step 1: Understand conversion method

    To convert strings to symbols, use the to_sym method on each string.
  2. Step 2: Apply map with to_sym

    Using names.map { |name| name.to_sym } converts each string to a symbol.
  3. Final Answer:

    names.map { |name| name.to_sym } -> Option B
  4. Quick Check:

    String to symbol conversion = B [OK]
Quick Trick: Use to_sym method to convert strings to symbols [OK]
Common Mistakes:
  • Using :name which creates same symbol repeatedly
  • Converting to string instead of symbol
  • Not using map for conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes