Giter VIP home page Giter VIP logo

interpolation-readme-v-000's Introduction

String Interpolation

Overview

We'll cover when and how to use string interpolation.

Objectives

  1. Interpolate variables into strings

When to Use String Interpolation

You're a party planner for Beyonce's 35th birthday and you're using Ruby to help you out with the arrangements. There is a variable called num_of_attendees and since she's very popular, this variable points to the integer 547. You try and print the value of num_of_attendees to the screen with the code below:

puts "There are num_of_attendees people coming to Beyonce's birthday party."

You expect this to print "There are 547 people coming to Beyonce's birthday party" but instead it prints "There are num_of_attendees people coming to Beyonce's birthday party." Why is this?

Well, that's because variables need to be interpolated inside a string to get their value, and not just referenced by their name, to print to the screen.

How You Interpolate Variables into Strings

To interpolate, you wrap the variable like #{this}.

Let's try again:

puts "There are #{num_of_attendees} people coming to Beyonce's birthday party."

This prints There are 547 people coming to Beyonce's birthday party.. Yay!

Additional Practice

Let's drop into IRB and copy and paste the code from the following example.

Let's say you have a super hard question on your biology test asking you to identify the technical term for a group of flamingos.

answer = "< fill in your answer here >"
puts "A group of flamingos is called a #{answer}."

Now, set the answer variable equal to "flamboyance" and run the following code in IRB:

answer = "flamboyance"
puts "A group of flamingos is called a #{answer}."

This prints A group of flamingos is called a flamboyance. to the screen.

Note that here you're declaring the variable answer before calling puts. You need to do it in this order, because our program is read by the computer sequentially. When your computer gets to #{answer}, it won't know what that is if answer isn't defined yet.

Another Way to Interpolate Variables into Strings

Some Rubyists write this another way, like this:

answer = "Flamboyance"
puts "A group of flamingos is called a " + answer + "."

There's debate about the best practice but most people at Learn think the first way looks nicer and is easier for your fellow programmers to read.

Note:

Interpolation will only work on Strings wrapped in double quotes "". Single quotes: '' do not support string interpolation, so running:

answer = 'Flamboyance'
puts 'A group of flamingos is called a #{answer}.'

Will just print A group of flamingos is called a #{answer}.

If you were committed to using single quotes in such a case, it would be the right time to use the alternative method ('A group of flamingos is called a ' + answer + '.') which would work just fine.

Just to clarify, this only applies to puts and other Ruby methods; it does NOT apply to variables. That is, answer can equal "Flamboyance" (double quotes) or 'Flamboyance' (single quotes) without affecting string interpolation.

View String Interpolation on Learn.co and start learning to code for free.

View String Interpolation on Learn.co and start learning to code for free.

interpolation-readme-v-000's People

Contributors

kthffmn avatar annjohn avatar sophiedebenedetto avatar deniznida avatar aviflombaum avatar morgvanny avatar mendelb avatar dougtebay avatar fislabstest avatar sdcrouse avatar sneathery avatar sarogers avatar markedwardmurray avatar loganhasson avatar drewprice avatar dvimont avatar pletcher avatar fs-lms-test-bot avatar

Watchers

James Cloos avatar

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.