ruby on rails - Converting string to title-case -
so i'm bit of noob in ruby , mission manually convert string title-case. have been able create method believe meet requirements assignments (it's not flexible, do), have hit 1 final snag. cannot seem join final array 1 string. code , rspec errors below. believe have use .join(" ") somewhere, not sure of exact syntax. in advance help!
my code:
class title attr_accessor :string def initialize(string) @string = string end def fix string.split(" ").each_with_index |value, index| if index >= 2 && value.length <= 3 value.downcase! else value.capitalize! end end end end
rspec:
expected: "the great gatsby" got: ["the", "great", "gatsby"] (compared using ==) exercise_spec.rb:6:in `block (3 levels) in <top (required)>'
again:
expected: "little red riding hood" got: ["little", "red", "riding", "hood"] (compared using ==) exercise_spec.rb:9:in `block (3 levels) in <top (required)>'
again:
expected: "the lord of rings" got: ["the", "lord", "of", "the", "rings"] (compared using ==) exercise_spec.rb:12:in `block (3 levels) in <top (required)>'
again:
expected: "the sword , stone" got: ["the", "sword", "and", "the", "stone"] (compared using ==) exercise_spec.rb:17:in `block (3 levels) in <top (required)>'
quick fix:
def fix string.split(" ").each_with_index |value, index| if index >= 2 && value.length <= 3 value.downcase! else value.capitalize! end value end.join(" ") end
if want ruby way of doing it:
def fix string.split(" ").map(&:capitalize).join(" ") end
Comments
Post a Comment