How do you capitalize every word in a string in Ruby?

How do you capitalize every word in a string in Ruby?

Ruby’s string class offers a few methods to change the case of a string.

  1. Upcase. Upcase is used to capitalize every letter of a string.
  2. Downcase. Downcase is used to make every letter in a string lowercase.
  3. Capitalize. Another method on the string class is capitalize.
  4. Titleize. Titleize capitalizes every word in a string.

What does TO_S do in Ruby?

to_s is defined on most objects and returns a string representation of this object. Defining to_str on an object is very much like saying “this object behaves like a string”. Calling to_str should return a string-like object, not juste a representation of the object as a string.

What is %W in Ruby?

%w(foo bar) is a shortcut for [“foo”, “bar”] . Meaning it’s a notation to write an array of strings separated by spaces instead of commas and without quotes around them.

When do you capitalize a string in Ruby?

capitalize is a String class method in Ruby which is used to return a copy of the given string by converting its first character uppercase and the remaining to lowercase.

How do you make a string lowercase in Ruby?

Downcase is used to make every letter in a string lowercase. Capitalize. Another method on the string class is capitalize. This method capitalizes the first letter of a string while downcasing the remaining letters.

How to capitalize every word in a string in rails?

Titleize capitalizes every word in a string. Remember you can only use this if you are working within the Rails framework (or just the ActiveSupport gem). name = “jaKe JoNes” name.titleize

How to access the end of a string in Ruby?

You can also access a single character from the end of the string with a negative index. -1 would let you access the last character of the string, -2 would access the second-to-last, and so on. Finally, you can convert the string to an array of characters with the chars method: