Blog

Exercise 6: Strings And Text: Learn Ruby The Hard Way: Practicum (TESTHEAD)

On November 5, 2011, in Syndicated, by Association for Software Testing
0

Well, first thing, Zed just updated the site, and some of the changes make it easier to navigate through to the other pages and some of the changes help to hi-light code sections and make the distinctions cleaner. So props to what looks to be a nice redesign :).

OK, so today we are going to be taking a look at “string interpolation” and some more techniques that you can use to make strings behave the way you want to and take advantage of some unique properties in Ruby. You can tell you are looking at a string when you see text in ‘single’ or “double” quotes.

We can use string formatters (that was the ‘%’ character and the designator that follows, see yesterday’s table for the list I found), but that has some overhead, including the fact that, if you want to do more than one entry, you need to use the bracket characters at the end of the line and separate the values by commas. Workable, yes, but less readable, too.

There is another way to put variables directly into a string, using that fancy term I mentioned before; string interpolation. That really just means “hey, when you see these characters together, it means take the variable value and replace it inside the string. The format for doing that is to use the “octothorpe” character (remember that from earlier in the week?) immediately followed by a left curly brace, the variable name, and then a closing curly brace. Like this: #{variable_name}. So the answer to the question from comments: When is a comment character not a comment character? when it’s paired with two curly braces. then it performs string interpolation. Woot!

In action:

The way we saw yesterday:

name1 = “Joe”
name2 = “Mary”
puts “Hello %s, where is %s?” % [name1, name2]

The String Interpolation way:

name1 = “Joe”
name2 = “Mary”
puts “Hello #{name1}, where is #{name2}?”

Even if you don’t quite get what’s going on, the second example is easier to read, even from a non coders perspective.

So our first exercise is as follows. Zed wants to have us write this out and tweak around with a bunch of variable substitiutions and print them to the screen. We also will be using shortened and abbreviated names. Remember when I said that longer, more descriptive names are helpful and make for readable code? I still stand by that, but for many, they like to make things short and tidy. Might as well get used to seeing it and working with it (but really) wnen possible, do with descriptive, it makes for code that self comments 🙂 ).

x = “There are #{10} types of people.”
binary = “binary”
do_not = “don’t”
y = “Those who know #{binary} and those who #{do_not}.”

puts x
puts y

puts “I said: #{x}.”
puts “I also said: ‘#{y}’.”

hilarious = false
joke_evaluation = “Isn’t that joke so funny?! #{hilarious}”

puts joke_evaluation

w = “This is the left side of…”
e = “a string with a right side.”

puts w + e

[and here is what we have]

What You Should See

There are 10 types of people.
Those who know binary and those who don’t.
I said: There are 10 types of people..
I also said: ‘Those who know binary and those who don’t.’.
Isn’t that joke so funny?! false
This is the left side of…a string with a right side.

[and that is indeed what I see]

Extra Credit

Go through this program and write a comment above each line explaining it.

[I think i got this right 🙂]

Find all the places where a string is put inside a string. There are four places.

[There are four places with literal “string” insertion (binary, do_not, x & y), but wait, there’s more 😉 ]

Are you sure there’s only four places? How do you know? Maybe I like lying.

[Since the (10) and the false are not in quotes, they are not literally strings in the defined sense, but since they are entered into the #{} directly with the case of 10 and false is passed for variable interpolation via #{hilarious}, it is treated like a string]

Explain why adding the two strings w and e with + makes a longer string.

[This is called concatenation, and it basically says take the first part and add it to the second part. Since it’s not mathematical or adding numbers, it interprets it as “take the first string and add the second string to it, not mathematically but put them side by side. If you have a better explanation, please comment 🙂 ].

TESTHEAD’s TAKEAWAYS

So that’s pretty cool. Any character value that we put between #{} will be presented and interpreted as a literal string. this makes sense as there are many Cucumber Web Steps that do exactly this, so it’s nice to see the official definition of how/why it is done this way. Also, interpolation can be encapsulated, meaning if you perform string interpolation to one variable, assign it to another, and do string interpolation a second time with the newly assigned variable, the original interpretation gets carried over.

 

Comments are closed.


Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!