Giter VIP home page Giter VIP logo

programming-univbasics-working-with-string's Introduction

Working With Strings

Learning Goals

  • Recognize how to declare a String with double quotes
  • Recognize how to declare a String with single quotes
  • State the difference between single- and double-quoted Strings
  • Define interpolation
  • Explain how different quote characters allow flexibility
  • Demonstrate escaping double quotes in a String
  • Join Strings using +
  • Identify why a TypeError happens when +ing a String with an Integer

Introduction

Thus far in programming as conversation, we've used numbers as data most of the time. Numbers are great because they reach across languages and cultures. But there are times when we need our programs to return information in the form of text. In this lesson we'll learn more about using text (i.e. Strings) in our Ruby expressions.

Recognize How to Declare a String with Double Quotes

We declare Strings most often by putting letters in double quotes:

String Declaration

Or, in Ruby:

greeting = "Hello, folks"

The letters inside of a String are often called "characters."

The pair of matching "s are called "String delimiters because they form a boundary or limit around the characters that make up the String.

Recognize How to Declare a String with Single Quotes

We can also declare Strings by putting the characters in single quotes:

Single Quote String

Or, in Ruby:

greeting = 'Hello, folks'

State the Difference Between Single- and Double-quoted Strings

For the most part, single and double quotes can be used interchangeably in Ruby. However, there is one instance where you need to use double quotes: when you are interpolating data into a String.

Define interpolation

When you use the interpolation operator you take data from the programming language, convert it to a String and then place that String inside of the String where the interpolation operator appeared.

The interpolation operator looks like this: #{}. When it appears in a double-quote-delimited String, the return value of the expression inside the operator is "plugged in" to the containing String.

In a single-quoted String there is no interpolation possible. Ruby simply sees #{} as "hash-mark, left-curly brace, right-curly brace."

bark_count = 3
double_q = "Byron barks #{bark_count} times" #=> "Byron barks 3 times"
single_q = 'Byron barks #{bark_count} times' #=> "Byron barks #{bark_count} times"

Here's a slightly more complex example to help you remember:

Interpolation with arithmetic

Explain How Different Quote Characters Allow Flexibility

What if you needed to store some dialog as a String:

In the book...

"Wait," said Jo, "Do not go without me!"

As a String for Ruby...

""Wait," said Jo, "Do not go without me!""

Since " is the String delimiter, Ruby would get confused if we gave it this String. It would attempt to end the String right before the W as the two "s "delimit" the String. Not what we wanted.

It might be a wise choice to use single quotes here.

little_woman_esque = '"Wait," said Jo, "Do not go without me!"'

Because the opening delimiter of the String was ', Ruby will "close" the String at the next ' โ€” at the very end. Inside of the single quotes, the " loses its meaning of "here's a String" and, instead, is just a plain literal, letter-like character ".

But oh my goodness, what if the speaker said Don't instead of Do not. That would break our String again as Ruby attempted to use the ' inside Don't as the closing delimiter.

What a mess. Sometimes we need to say "Don't use this ' or " as a String delimiter. To do this we need escaping.

Demonstrate Escaping Double Quotes in a String

So, what are we to do when we need interpolation but also have embedded double quotes? We can "escape" the power of " to close a String by putting a \ in front of it:

character = "Amy"
little_woman_esque = "\"Wait,\" said #{character}, \"Do not go without me!\""

Since the delimiting character is ", we should close the String immediately before the W. BUT since there is a \ immediately before the second ", thus escaping it, Ruby says "Oh you mean to use this as a character, not as a String delimiter. I'll find the next unescaped "."

It doesn't find an unescaped " until the very end, just like we want.

By the way, we could also change the word to "Do not" to "Don't" as well since ' is not being used as a delimiter.

Join Strings using +

Now here's an interesting use of the + operator! When placed between Strings, it joins them and returns a new String.

Try this out in IRB (or make up your own variation):

first_name = "Byronius"
clan_name = "Karbitus"
common_name = "Maris"

# With +
full_name = first_name + " " + clan_name + " " + common_name #=> "Byronius Karbitus Maris"

# Or, with interpolation
full_name = "#{first_name} #{clan_name} #{common_name}" #=> "Byronius Karbitus Maris"

# Keep in mind it returns a _new_ String; therefore:
first_name  #=> "Byronius"
clan_name   #=> "Karbitus"
common_name #=> "Maris"
full_name   #=> "Byronius Karbitus Maris"

Identify Why a TypeError Happens When +ing a String with an Integer

Be careful, + can only join two Strings! To work, both sides must be of type String:

fact = "Byron is "
tail = " years old"
age = 5

fact + age + tail #=> TypeError (no implicit conversion of Integer into String)

Ruby is not sure whether you want to add like an Integer (stored in age) or add like Strings (stored in fact and tail)! If you need to do this, you should use the to_s method on data (like 5) OR use the interpolation operator #{} which, as we said earlier, does the calculation and converts the result to a String automatically.

fact = "Byron is "
tail = " years old"
age = 5

fact + age.to_s + tail  #=> "Byron is 5 years old"
"#{fact} #{age} #{tail}" #=> "Byron is 5 years old"

Most data types feature a method called .to_s ("to String") which turns them into a String. Once a non-String is converted to a String, it can be joined with + to another String.

You might still be unclear on what a "method" is. You might remember the .class method we introduced when we were talking about type earlier. For now, think of methods as "commands" you can ask values to do in Ruby.

Conclusion

With mastery of String, we can use the expressions we've learned thus far to make interesting programs using only expressions. We're going to provide an example in the next lesson.

programming-univbasics-working-with-string's People

Contributors

lizbur10 avatar maxwellbenton avatar sgharms avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

programming-univbasics-working-with-string's Issues

example missing the +

Join Strings using +
Now here's an interesting use of the + operator when placed between Strings, it joins them and returns a new String.

first_name = "Byronius"
clan_name = "Karbitus"
common_name = "Maris"

With +

first_name + " " + clan_name + " " common_name #=> "Byronius Karbitus Maris"

Or, with interpolation, like you already know

"#{first_name} #{clan_name} #{common_name}" #=> "Byronius Karbitus Maris"

Hello,
The example above for # With + is missing the + between " " and common_name. I kept getting an error until I added it myself.

Thank you,
Matthew Van Eck

possible error..

Identify why TypeError Happens When + String with Integer
fact = "Byron is "
tail = " years old"
age = 5

fact + age + tail #=> TypeError (no implicit conversion of Integer into String)
Ruby is not sure whether you want to add like an Integer (stored in tail) or add like Strings! If you need to do this, you should use the to_s method on data.

I think the integer is stored in AGE and not in Tail per the text..

A missing "

In the below example right before the conclusion:
fact = "Byron is "
tail = " years old"
age = 5

fact + age.to_s + tail #=> "Byron is 5 years old"
"#{fact} #{age} #{tail} #=> "Byron is 5 years old"

"#{fact} #{age} #{tail} #=> "Byron is 5 years old" should be "#{fact} #{age} #{tail} #"=> "Byron is 5 years old" correct?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.