1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

String Quick Reference


String Quick Reference

Strings are perhaps the most used class in Ruby. And with over 100 methods in the String class alone, it can be a bit daunting. Intimidating, even. This article will cut through all the noise and just show you how to do the things you need to do with strings.

Creating Strings

Strings are most often created with a String literal. There are two major variants of the String literal.

  • Single-quoted string literals don't do much except put the characters between the quotes in the string. For example, the string literal 'this is a string' does just what it looks like, makes a string with those characters. Single quoted strings go out of their way to throw as few surprises and curveballs at you, so even strings like 'test\nstring' will produce just that, the newline escape sequence will not be interpreted, a backslash and an n character will be generated from that sequence.
  • Double-quoted strings are the magic variant. They do things like escape sequences, string interpolation, etc. The string "this is a string" is equivalent to the above example, but the string "test\nstring" will interpret the escape sequence and produce a newline character. String interpolation is also an important topic, and is covered below.

Reading and Writing Strings

  • Reading strings from the keyboard is done via the gets (get string) method. This is short for STDIN.gets and will read a complete line from standard input (including the newline). If we have line = gets, the program will block until the user inputs a line and presses the enter key. To get rid of the newline, you can use the line = gets.chomp idiom.
  • Writing strings to standard output is done via the puts (put string) method. This method takes, among other variants, a single string to be output. It will output that string as well as a newline character, there's no need to embed them in your output strings. The typical "hello world" example in Ruby would read puts "Hello world". If you wish to print something without the newline, you can use the print method. To do the same as the above example using print, you can say print "Hello world\n".

String Interpolation and Formatting

Straight up string literals are boring, eventually you'll want to put something more interesting in them. There are two primary ways of doing that in Ruby.

  • String interpolation allows you to insert any Ruby code in the middle of a string encapsulated in #{ … } and whatever that Ruby code evaluates to will be inserted in that string. For example, if you have a = 10, you can say str = "The value of a is #{a}". Note that this isn't a simple variable name, that's like a mini Ruby block, so any Ruby code can go in there. You can call a method, do some math, etc, so you can even say str = "10 plus the value of a is #{10 + a}".
  • String formatting is a little more traditional. Most languages don't have a mechanism for doing something like string interpolation. Ruby does printf-style format strings as well. If you want to insert a formatted integer a = 10 into a string, you can say something like str = "The value of a is %05d" % a and Ruby will produce the string "The value of a is 00010". You can add on as many arguments as you wish by passing them in an array, so you can say a, b = 10, 20 and produce the string str = "The values of a and b are %d and %d" % [a, b].

Splitting and Iterating Strings

For formatted input, you may be used to iterating over the string, finding a separator, copying out the characters before the separator, etc. In Ruby, things are a bit easier.

  • Splitting strings refers to the process of separating a string into an array of strings via some delimiter. This is done with the "some string".split method. It generally takes a single argument, either a string or regular expression describing the delimiter. Note, the delimiters are discarded, and will not be part of the output array at all. So "some:string".split(":") will return the array ["some", "string"]. Using a regular expression, you can be a bit more flexible with your delimiters. For example, you can split on any number of colons using a regular expression. If you were to say "some::::string".split(":"), it'll produce the array ["some", "", "", "", "string"], but "some::::string".split(/:+/) will produce the expected array of ["some", "string"]. Finally, if you use a single space as your delimiter, even if it's just in a string and not a regular expression, it will match whitespace of any size, including newlines, so even "some\nstring".split(' ') will produce the expected output of ["some", "string"].
  • Iterating over strings is a bit trickier in Ruby. In general it's not something you'll need to do often, but iterating over a string raises an interesting question: What is a character? Simple, it's an 8-bit value, right? Maybe if you're used to C, but Ruby uses UTF-8 by default. This is a form of unicode that's mostly-compatible with ASCII. This means most characters (assuming your strings are in English) will be normal 8-bit values, but any accented or foreign characters will be multibyte characters. So if you want to iterate over the bytes in a string, you can use the each_byte method, like so: "some string".each_byte{|b| puts b } to iterate over the string as a list of integers. If you want to iterate over a list of characters, you can use the each_char method in a similar fashion. Instead of integers as with each_byte, the each_char method will pass each character in the string as a single-character string to the block.
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. String Quick Reference

©2014 About.com. All rights reserved.