Ruby on Rails : StringsA
object holds and manipulates an arbitrary sequence of bytes, typicallyrepresenting characters. String objects may be created using
or as literals.Because of aliasing issues, users of strings should be aware of the methods that modifythe contents of a
Methods with names ending in ``!’’ modify their receiver.
Methods without a ``!’’ return a new
.Ruby deals with strings as well as numerical data. A string may be double-quoted ("...")or single-quoted ('...').
A double-quoted string allows character escapes by a leading backslash, and theevaluation of embedded expressions using
A single-quoted string does not do this interpreting; what you see is what you get.Examples:If you had done the above mentioned experiments, it is easy to understand the above two points. The difference between the double and single quotes around a string. There are plenty of methods that ruby offers, they come very handy!
str.capitalize => new_str This method turns the first letter of the string to upper case.
"hello".capitalize #=> "Hello"
str.chomp(separator=$/) => new_str Returns a new
with the given record separator removed from the end of
(if present). If
has not been changed from the default Ruby record separator, then
also removes carriage return characters (that is it will remove
"ruby\r\n".chop #=> "string""ruby\n\r".chop #=> "string\n"
3Prepared By:Sumanth Krishna. A