# Checks if a given string is in title case (every word starts with an uppercase letter
# and has lowercase remaining letters).
#
# @param input_string [String] The string to check.
# @return [Boolean] true if the string is in title case, false otherwise.
# @raise [TypeError] If the input is not a String.
def title_case?(input_string)
# Guard clause: validate input type
unless input_string.is_a?(String)
raise TypeError, "Input must be a valid String."
end
# Split the string into individual words by whitespace
words = input_string.split
# Return false if the string is empty or contains only whitespace
return false if words.empty?
# Check if every word matches its capitalized form (e.g., "Hello" == "Hello")
words.all? { |word| word == word.capitalize }
end
# --- Test Cases Demonstration ---
if __FILE__ == $PROGRAM_NAME
test_cases = [
"Hello World",
"Hello world",
"hello World",
"Ruby Programming Language",
"This Is Fine",
"This is Not Fine"
]
test_cases.each do |test_case|
result = title_case?(test_case)
puts "#{test_case.inspect} => #{result}"
end
end
# run:
#
# "Hello World" => true
# "Hello world" => false
# "hello World" => false
# "Ruby Programming Language" => true
# "This Is Fine" => true
# "This is Not Fine" => false
#
#