Menu
CoddyTech

Case and Ranges

Lesson 10 of 48 in Coddy's Ruby for R Developers course.

R switch distinguishes character names from numeric positions, while Ruby case uses case equality and supports range matchers. R colon sequences include their endpoints and can descend, whereas Ruby three-dot ranges exclude the endpoint and two-dot ranges include it. Test empty-range assumptions explicitly instead of translating 1:length patterns mechanically. Ruby chooses the first matching case branch.

R: complete program

value <- "2"; result <- if(identical(value,2L)) "integer" else if(identical(value,"2")) "text" else "other";cat(result,"\n",sep="")

Ruby: complete program

letter = "e"
puts(case letter
when "a", "e", "i", "o", "u" then "vowel"
else "other"
end)
puts (2...5).to_a.join(" ")
challenge icon

Challenge

Easy

Read an integer score 0..100. Return low for 0..39, middle for 40..79, high for 80..100.

Return text from solve(input) without printing. The locked driver prints the returned text with one final newline and checks required public APIs independently.

Your code goes in exercise.rb. main.rb holds the supplied input/output code and cannot be edited.

Try it yourself

require_relative 'exercise'
def check(value)
  raise 'Public API check failed' unless value
end

puts solve(STDIN.read)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Ruby for R Developers

Practice on your own: Online Ruby compiler