0
0
Rubyprogramming~10 mins

Split and join methods in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to split the string into an array of words.

Ruby
words = "hello world ruby".[1](" ")
Drag options to blanks, or click blank then click option'
Ajoin
Bconcat
Csplit
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'join' instead of 'split' which combines arrays into strings.
Forgetting to provide a separator.
2fill in blank
medium

Complete the code to join the array elements into a single string separated by commas.

Ruby
result = ["apple", "banana", "cherry"].[1](",")
Drag options to blanks, or click blank then click option'
Aselect
Bsplit
Cmap
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'split' which breaks strings instead of joining arrays.
Not providing a separator string.
3fill in blank
hard

Fix the error in the code to split the string by commas.

Ruby
items = "red,green,blue".[1](",")
Drag options to blanks, or click blank then click option'
Ajoin
Bsplit
Cconcat
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'join' which is for arrays, not strings.
Using 'replace' which changes content but does not split.
4fill in blank
hard

Fill both blanks to create a hash where keys are words and values are their lengths for words longer than 3 letters.

Ruby
lengths = {}; words.each { |word| lengths[[1]] = [2] if word.length > 3 }
Drag options to blanks, or click blank then click option'
Aword
Bword.length
Cword.size
Dword.upcase
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.size instead of word.length (both work but only one is correct here).
Using word.upcase which changes the word but is not length.
5fill in blank
hard

Fill all three blanks to create a string of uppercase words joined by a dash, only including words longer than 3 letters.

Ruby
result = words.select { |w| w.length > [1] }.map { |w| w.[2] }.[3]("-")
Drag options to blanks, or click blank then click option'
A3
Bupcase
Cjoin
Ddowncase
Attempts:
3 left
💡 Hint
Common Mistakes
Using length > 4 instead of > 3 changes the filter.
Using downcase instead of upcase.
Using split instead of join at the end.