Instructions
Create a function titleize that converts a string to title case,
where the first letter of each word is capitalized.
Example: titleize("hello world") returns "Hello World"
Hints:
- Split into words, capitalize each, join back
- Use .split, .map, and .join
- Each word: word.capitalize
Your Code
def titleize(str)
str.split.map(&:capitalize).join(" ")
end
RSpec.describe "titleize" do
it "titleizes hello world" do
expect(titleize("hello world")).to eq("Hello World")
end
it "handles mixed case" do
expect(titleize("hELLO wORLD")).to eq("Hello World")
end
it "handles single word" do
expect(titleize("ruby")).to eq("Ruby")
end
end
Results
Click "Run Tests" to see results