More on Strings
There are many methods in the String class (you don't have to memorize them all; you can look up the documentation) like the reverse that gives a backwards version of a string (reverse does not change the original string). length that tells us the number of characters (including spaces) in the string. upcase changes every lowercase letter to uppercase, and downcase changes every uppercase letter to lowercase. swapcase switches the case of every letter in the string, and finally, capitalize is just like downcase, except that it switches the first character to uppercase (if it is a letter), slice gives you a substring of a larger string.
The methods upcase, downcase, swapcase and capitalize have corresponding methods that modify a string in place rather than creating a new one: upcase!, downcase!, swapcase! and capitalize!. Assuming you don't need the original string, these methods will save memory, especially if the string is large.
We know that String literals are sequences of characters between single or double quotation marks. The difference between the two forms is the amount of processing Ruby does on the string while constructing the literal. In the single-quoted case, Ruby does very little. In the double-quoted case, Ruby does more work. First, it looks for substitutions - sequences that start with a backslash character - and replaces them with some binary value. The second thing that Ruby does with double-quoted strings is expression interpolation. Within the string, the sequence #{expression} is replaced by the value of expression (refer p013expint.rb . In this program, the value returned by a Ruby method is the value of the last expression evaluated, so we can get rid of the temporary variable (result) and the return statement altogether.
| def say_goodnight(name) result = "Good night, #{name}" return result end puts say_goodnight('Satish') # modified program def say_goodnight2(name) "Good night, #{name}" end puts say_goodnight2('Talim') |
It is to be noted that every time a string literal is used in an assignment or as a parameter, a new String object is created.
How is memory managed for Strings in Ruby? Is there a separate pool for Strings? Strings are objects of class String. The String class has more than 75 standard methods. If you refer to Ruby User's Guide, it says that "we do not have to consider the space occupied by a string. We are free from all memory management."
Listing all methods of a class or object String.methods.sort
shows you a list of methods that the Class object String responds to.
String.instance_methods.sort
This method tells you all the instance methods that instances of String are endowed with.
String.instance_methods(false).sort
With this method, you can view a class's instance methods without those of the class's ancestors.
Comparing two strings for equality
Strings have several methods for testing equality. The most common one is == (double equals sign). Another equality-test instance method, String.eql?, tests two strings for identical content. It returns the same result as ==. A third instance method, String.equal?, tests whether two strings are the same object. An example p013strcmp.rb illustrates this:
# String#eql?, tests two strings for identical content. It returns the same result as == # String#equal?, tests whether two strings are the same object s1 = 'Jonathan' s2 = 'Jonathan' s3 = s1 if s1 == s2 puts 'Both Strings have identical content' else puts 'Both Strings do not have identical content' end if s1.eql?(s2) puts 'Both Strings have identical content' else puts 'Both Strings do not have identical content' end if s1.equal?(s2) puts 'Two Strings are identical objects' else puts 'Two Strings are not identical objects' end if s1.equal?(s3) puts 'Two Strings are identical objects' else puts 'Two Strings are not identical objects' end
|
Exercise
Given a string, let us say that we want to reverse the word order (rather than character order). You can use String.split, which gives you an array of words. The Array class has a reverse method; so you can reverse the array and join to make a new string:
words = 'Learning Ruby - Your one stop guide' puts words.split(" ").reverse.join(" ") # guide stop one Your - Ruby Learning
|
Refer the Complete String documentation