Let’s talk about STRING theory…(Not really)

String theory refers to the mathematical models which seek to find a common explanation for the four main forces seen in nature. These forces are the electromagnetic force, the strong and weak nuclear force and gravity. But this isn’t a post on the String theory, but the STRING class. What do these two have in common? Well, do we really know what either the string theory or string class are? We think we know, but when we hit a roadblock working with either of these topics, we tend to ask ourselves what the hell’s going on.

With the STRING class, it’s surprising what we know, or rather, what we think we know. Let’s start with the idea of creating strings. It’s commonly known that strings can be created in either single- or double-quotes or nested within each one another.

  a_string_with_quotes = "She said, 'This is a string!' "

Strings have some added features such as escaping certain characters or performing string interpolation.

  artist = "Adele"
  song = "Hello"
  puts "Song of the year by: #{artist} - #{song} \n"

It’s great that we can mix single- or double-quotes in a string, but that can be tedious to write.

  str = '"Stop", she said, "I can\'t live without \'s and "s."'

can be rewritten as

  str = %q{'"Stop", she said, "I can\'t live without \'s and "s."'}

%q and %Q is the choice between single- or double-quotes.

Strings are also multiline…

  str = %Q{This is a multiline \ #trailing backslash removes newline character

and can be rewritten as

  str = <<EOF
   This is a multiline
   string. I can't believe

Strings of course have methods as well. What’s interesting about the String class, is that it doesn’t have the #each method you commonly see with some other classes. The question we ask about the #each method is what type of collection are we actually trying to go through.

If you see strings as a collection of chars, you can use #each_char method, but strings can also be seen as a collection of bytes, thus the #each_byte method. And going back to multiline strings, we can use the #each_line method.

Strings are also mutable.

  first_name = "james"
  upcased = first_name

# Will alter both Variables
  puts first_name => "JAMES"
  puts upcased => "JAMES"

# Will only alter the first variable, with the second unchanged
  first_name = first_name.upcase 
  puts first_name => "JAMES"
  puts upcased => "James"

There are many things we may not know about the STRING class, but take comfort in the fact that we have a better chance of learning more about this class than string theory.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s