Stringing Things Along

As with other sections, the official docs are a good place to look if you want to jump in a bit deeper and red-pill the chapter.

Docs

What a Character

What's a String? Well a String is just a collection of characters like "a" or "b" or "pumpernickel bread". Oh yes, we have "rye humour" as well, and would you look at that? It's a String too!

To make a string, we just need a few quotes:

No no no, not that type of quote, this type:

2.4.2 :001 > "To be or not to be, that is the question"
 => "To be or not to be, that is the question"

...well yes, now we have a quote in quotes, very clever.

Well you're speaking French to me.

Well we are talking about String literals.

Concat got your toungue?

Strings, like Numbers, get math operations too! We can add them, multiply them, and a few other operations:

2.4.2 :002 > question = "that is the question."
 => "that is the question."

2.4.2 :003 > "To be" + " or not to be, " + question
 => "To be or not to be, that is the question."

Read that real quick, we're just adding strings together. Notice the spaces though. Forgetting those means we get some really odd looking strings.

Granted we'll rarely if ever add strings together in Ruby, but that's for a different reason we'll see next.

What happens if we try and multiply them? What do you think will happen?

Let's give it a try!

2.4.2 :004 > "Na" * 8 + " LemurMan!"
 => "NaNaNaNaNaNaNaNa LemurMan!"

Bat got your tongue? Well tell it to give it back, though nicely. It's not a particularly nice bat, no no no.

Interpolation Station

So why don't we add strings together in Ruby? Turns out there's a nicer way, we just tell the string we want to shove something into it!

2.4.2 :001 > quote = "Brevity is the soul of wit"
 => "Brevity is the soul of wit"
2.4.2 :002 > author = "Polonius"
 => "Polonius"
2.4.2 :003 > "#{quote} - #{author}"
 => "Brevity is the soul of wit - Polonius"

Though the quote itself isn't without some irony considering Polonius rattled on quite a bit before getting to the danged point, kinda like what we're doing now! Ack!

Polonius. Interpolation. Interpolonius? No no no, that won't do at all. What a Hamlet.

Interpolation just means we're sticking a value inside a string, and all we need to do is slap a # in front of the value surrounded by a few brackets {}. Really you could throw anything into there:

2.3.3 :001 > "#{1} #{'maybe a string?'} #{3.50}"
 => "1 maybe a string? 3.5"

It just asks each value how it'd like to be shown as a string by asking it to make itself into one with to_s, kinda like to_f earlier asked that Number to be a Float for us. Mighty kind of it to oblige, no?

Literally

You might think that we can use '' single quotes and "" double quotes interchangeably. Well that's not entirely accurate.

Single quotes are literal. That means if we try and interpolate or put any special characters in there it'll literally give that back to us:

2.4.2 :004 > variable = 'foobar'
 => "foobar"
2.4.2 :005 > string = 'This is a #{foobar}\n'
 => "This is a \#{foobar}\\n"

You'll notice it returned the entire interpolation like originally written. With double quotes Ruby will look to see if we put anything special in there and substitute it out.

Also it's typically a bad idea to use the same quotes inside of themselves:

"This is a "bad idea""

Now you have bad idea sitting there completely outside of quotes because you've gone and closed the original. Notice how the color changes. If you actually did this you'd get a nice error too:

2.4.2 :006 > "This is a "bad idea""
SyntaxError: (irb):6: syntax error, unexpected tIDENTIFIER, expecting end-of-input
"This is a "bad idea""
               ^
    from /Users/lemur/.rubies/ruby-2.4.2/bin/irb:11:in `<main>'

Now you can always escape those quotes to fix the issue, but that does get tedious:

2.4.2 :007 > "This is a \"bad idea\""
 => "This is a \"bad idea\""

If this confuses you, just use double quotes for everything while starting out as the difference is fairly small. I'll be using double quotes in most places for this reason.

Putsing this out there

Now this one will come up some time when you're trying to get the value out of something and you use print or puts. They both return nil when called:

2.4.2 :008 > puts 1
1
 => nil

It'll look like it might have returned the value as it prints it out to the screen, but it's nil as sure as sunshine. This one will likely bite you in later chapters.

Objection!

Remember that bit that everything in Ruby is an Object?

Ok ok ok, most everything, but that's not important for now. What that means is we can ask a string questions and tell it to do thing just like we can with numbers:

2.4.2 :009 > "string".reverse
 => "gnirts"
2.4.2 :010 > "string".size
 => 6
2.4.2 :011 > "".empty?
 => true
2.4.2 :012 > "brandon".capitalize
 => "Brandon"
2.4.2 :013 > "WHY ARE WE SHOUTING?".downcase
 => "why are we shouting?"
2.4.2 :014 > "WHY ARE WE SHOUTING?".downcase.capitalize
 => "Why are we shouting?"

We can do all types of fun things with Strings, and the nice thing is Ruby is kind enough to name all of these methods in a way it's pretty straightforward what we're doing with them.

The ones to watch out for are any of them that mention anything about Regexp, as they're a certain Lemur's absolute favorite subject, and not the one you'd like to see.

While Regexp can be a bit hard to understand, it's incredibly useful for later. So much in fact that we'll dedicate an entire chapter to it later on in Tyrannosaurus RegEXp.

Let's Split!

Words are fun, I like words, and I like to use a lot of them. How do we go and get us some words out of a string? We tell it to split!

2.4.2 :015 > "I like words".split
 => ["I", "like", "words"]

Oh hey, that looks like more than a few words. What's that it's in there? Well that's called an Array, but we'll get to those later. Short version is it's a collection of some things.

Split likes to take arguments though, and what that means is we can argue with it on what to split on:

2.4.2 :016 > "I like words".split(' like ')
 => ["I", "words"]

You could make it split on anything!

Why's it called an argument though? Well we have just the right Lemur to tell us, don't we?

An argument is thusly named because of its roots in Mathematics. In the general sense it means something by which another thing may be deduced.

That's wordy speak for saying it listens to our opinions we tell it and changes its answers based on what we tell it.

A function (which we'll also cover later) can take one argument, many arguments, no arguments, or a lot of different types of them. Sometimes we can even tell them what we probably meant if we forget to give it one.

Feeling Included

What if we want to know if a String has something in it? We could just ask:

2.4.2 :017 > 'foobarbaz'.include?('bar')
 => true
2.4.2 :018 > 'foobarbaz'.include?('bars')
 => false

include? will check if our string includes another one.

What if we want to make sure the string not only includes another one, but starts with it? We'd check start_with?:

2.4.2 :019 > 'foobarbaz'.start_with?('foo')
 => true
2.4.2 :020 > 'foobarbaz'.start_with?('bar')
 => false

Same deal with end_with?:

2.4.2 :021 > 'foobarbaz'.end_with?('baz')
 => true
2.4.2 :022 > 'foobarbaz'.end_with?('bar')
 => false

There are some more powerful ways in Ruby to check if a String contains something, but we'll save that for our chapter on RegExp: Tyrannosaurus RegEXp.

results matching ""

    No results matching ""