You are here: CodeIdol.com > Other > Ruby Cookbook > Strings
Ruby Cookbook
| Recipe 1.1. Building a String from Parts
Problem
You want to iterate over a data structure,
building a string from it as you do.
Solution
There are two efficient solutions. The simplest solution is to start with an empty string, a...
|
|
| Recipe 1.2. Substituting Variables into Strings
Problem
You want to create a string that contains a representation of a Ruby variable or expression.
Solution
Within the string, enclose the variable or expression in curly bracket...
|
|
| Recipe 1.3. Substituting Variables into an Existing String
Problem
You want to create a string that contains Ruby expressions or variable substitutions, without actually performing the substitutions. You plan to substitute values into the strin...
|
|
| Recipe 1.4. Reversing a String by Words or Characters
Problem
The letters (or words) of your string are in the wrong order.
Solution
To create a new string that contains a reversed version of your original string, use the revers...
|
|
| Recipe 1.5. Representing Unprintable Characters
Problem
You need to make reference to a control character, a strange UTF-8 character, or some other character that's not on your keyboard.
Solution
Ruby gives you a number of escaping m...
|
|
| Recipe 1.6. Converting Between Characters and Values
Problem
You want to see the ASCII code for a character, or transform an ASCII code into a string.
Solution
To see the ASCII code for a specific character as an integer, use th...
|
|
| Recipe 1.7. Converting Between Strings and Symbols
Problem
You want to get a string containing the label of a Ruby symbol, or get the Ruby symbol that corresponds to a given string.
Solution
To turn a
symbol into a string, use
...
|
|
| Recipe 1.8. Processing a String One Character at a Time
Problem
You want to process each character of a string individually.
Solution
If you're processing an ASCII document, then each byte corresponds to one character. Use String#each_byt...
|
|
| Recipe 1.9. Processing a String One Word at a Time
Problem
You want to split a piece of text into
words, and operate on each word.
Solution
First decide what you mean by "word." What separates one word from another? Only whitespace? Wh...
|
|
| Recipe 1.10. Changing the Case of a String
Problem
Your string is in the wrong case, or no particular case at all.
Solution
The String class provides a variety of case-shifting methods:
s = 'HELLO, I am not here. I WENT to tHe Ma...
|
|
| Recipe 1.11. Managing Whitespace
Problem
Your string contains too much whitespace, not enough whitespace, or the wrong kind of whitespace.
Solution
Use
strip to remove whitespace from the beginning and end of a string:
" \tWhi...
|
|
| Recipe 1.12. Testing Whether an Object Is String-Like
Problem
You want to see whether you can treat an object as a string.
Solution
Check whether the object defines the
to_str method.
'A string'.respond_to? :to_str # => t...
|
|
| Recipe 1.13. Getting the Parts of a String You Want
Problem
You want only certain pieces of a string.
Solution
To get a substring of a string, call its
slice method, or use the array index operator (that is, call the [] method). Either...
|
|
| Recipe 1.14. Handling International Encodings
Problem
You need to handle strings that contain nonASCII characters: probably
Unicode characters encoded in UTF-8.
Solution
To use Unicode in Ruby, simply add the following to the begi...
|
|
| Recipe 1.15. Word-Wrapping Lines of Text
Problem
You want to turn a string full of miscellaneous whitespace into a string formatted with linebreaks at appropriate intervals, so that the text can be displayed in a window or sent as an email...
|
|
| Recipe 1.16. Generating a Succession of Strings
Problem
You want to iterate over a series of alphabetically-increasing strings as you would over a series of numbers.
Solution
If you know both the start and end points of your successi...
|
|
| Recipe 1.17. Matching Strings with Regular Expressions
Problem
You want to know whether or not a string matches a certain
pattern.
Solution
You can usually describe the pattern as a regular expression. The =~ operator tests a string ag...
|
|
| Recipe 1.18. Replacing Multiple Patterns in a Single Pass
Problem
You want to perform multiple, simultaneous search-and-replace operations on a string.
Solution
Use the
Regexp.union method to aggregate the regular expressions you want ...
|
|
You are here: CodeIdol.com > Other > Ruby Cookbook > Strings
ADBRITE ads links
|
|
Related tags
Popular Categories
|