DEV Community


Strings in Ruby

Dev. I love dogs, cookies, and learning new things.
Originally published at on ・4 min read

Today we are going to talk about strings, specifically in Ruby: What are they? How do I make them? What can I do with them? By the end, both of us should have a lot of good information in one place so we don’t have to hunt for it later.

What is a string?

A string, at its most fundamental level, is a collection of bits and bytes. At a people-level, it’s the letters and other characters we use to make sentences. When I think of strings, I think of sentences first. Strings can be a lot of things: a letter, a punctuation mark, a space, jibberish — but it’s code that doesn’t represent something else, unlike a variable or a class.

For example:

Good\_Dog = Waffles

Now, Good_Dog points to my dog’s name, Waffles. “Waffles” is a string, and the value of the variable Good_Dog.

This is Waffles. He is, indeed, a good dog.

Strings are important because they let us speak to our users. “Hello World!” is a string. So are“Dogs are great!” and “Waffles is a good boy.” and “a;slkdjaw;ortu0q24”.

How do I make them?

Strings can be produced in a couple of different ways. We can use the single quotation mark: ‘Dogs are great.’ OR we can use the double quotation mark: “Dogs are great.”

What’s the difference?

  • With a single quotation mark, you’re limited in what you can do. You can’t use things like \t or \n, and you can’t interpolate. A single quotation mark string is simple, but we want so much more than this provincial line.
  • With a double quotation mark, you get freeeeedom. They can do everything single quote strings can do, but more. Most helpful is the ability to interpolate, or incorporate other expressions into your string.

What can I do with them?

ALL the things. You can count the characters! You can drop in the results of methods! You can execute methods on them! You can figure out whether a character has more than one byte! You can iterate over them! SO MANY OPTIONS. And because I’m a n00b and I need to start retaining these things, we’re going to go over them. For the examples below, we’re going to use str to represent string.

  • Encoding : Ruby supports many encoding styles. You can find out what style your string (or anyone else’s string) is using You can also change a string’s encoding style. You can (1) set the encoding to something else “without changing the internal byte representation of the string” or (2) transcode, or “translate its internal byte representation to another encoding.”

str.force_encoding(Encoding: :UTF_8) str.encode!(Encoding: :UTF_8)

  • Indexing: When you think about it, a string is just a list of characters. “abcde” or “d-o-g-” or “cat”. What are other lists that we know how to deal with? Arrays! Much like arrays, you can index a string and iterate over a string (below). Like an array, the index starts at 0 and the last character is going to be -1. So using “Waffles”:
str = "Waffles" str[0] "W" str[-1] "s" str[-2] "e"
  • Extracting: You can pull out specific characters or a specific group of characters from a string. The syntax is str[the character you’re starting at, how many characters from that first character (including the first character) you are moving].
str = "Waffles"

# Below, we start at "W" at index 0, 
# and then _including_ "W", we move 2 spaces. 

# Next, we start at "l" at index 4, 
# and then _including_ "l", we move 3 spaces.
  • Inserting and Deleting : Like arrays, you can add items to and delete items from a string.
  • Iterating: There are several ways to iterate over a string: each_byte, each_char, each_codepoint, and each_line are the lines I have learned about so far. Their functions are pretty obvious from their names, and syntactically they are used the way we use iterations in arrays.
  • .chomp: Om nom nom. Chomping will remove the given separator from the end of the string if one is present as well as carriage return separators. You can also chomp off what you enter as an argument in parentheses, below:
\> "Waffles is a good boy!".chomp("boy!")
\> "Waffles is a good"
  • .strip: This will remove the leading and trailing white space from a string.
\> " Waffles is a good boy! ".strip
\> "Waffles is a good boy!"
  • .chop: Chop will give you a new string with the last character removed. Carriage returns are treated as a character so if they aren’t present, a character from the string will be removed.
\> "Waffles".chop
\> "Waffle"

That’s enough about strings for now. The good thing about writing though is that I can come back to this an add whatever I need to whenever I need to. Once I’m done reviewing the next chunk of material, we’ll come back and over that too. ❤

Discussion (0)