Ruby Intro - Strings Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Either double or single quotes can be used to protect text. Why would you use one over the other?

A

To include quotation marks as strings into your text (you’d need to use the single quotes then).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s the rule using escape characters or string interpolation with quotes?

A

Escape characters and string interpolation work within a double quoted string but not within single quoted string. For instance /n creates a new line, but only in a double quoted string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is best way to protect strings with lots of escape characters and quotes.

A

Use double quotes to protect the string. Then use escape characters each time to show any quote whatsoever.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is interpolation or concatenation better to build up multiple strings together?

A

Usually interpolation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is String Interpolation? Give an example.

A

Putting a string inside a string. Inside the #{}, you place ruby code; the code is executed, and the return value is inserted inside the string.

worst_day = “Monday”

"#{worst_day}s are the hardest."
# => "Mondays are the hardest."
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the readability rule when using String Interpolation

A

The code inside the #{} should be very short; just one variable plus maybe a single method call. Otherwise, first save the result in a temporary variable, then interpolate that:

MURDER = “redrum”.reverse.upcase

”#{MURDER}! #{MURDER}!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you append strings to strings? Two methods, Code examples.

A

count_in = “”
count_in &laquo_space;“One, “
count_in &laquo_space;“two, “
# …

count_in = ""
count_in = count_in + "One, "
count_in = count_in + "two, "
# ...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you find out what substring is in what specific position/index of your code?

A
"this is my sentence"[5..6]
# => "is"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you find character length of a string? code

A
"words words words".length
# => 17
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

If you wanted to split up a string, what would you want to do first? How would you code it?

A

=> [“Bi-Rite”, “Humphrey Slocum”, “Mitchell’s”]

You would want to split the string up into an ARRAY of split elements.

ice_creams = “Bi-Rite, Humphrey Slocum, Mitchell’s”

ice_creams.split(“, “)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you purge string or create empty string via code (not using “ “)?

A

nil.to_s

=> “”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The methods for removing returns and leading and trailing spaces in a string?

A

chomp and #strip (#chomp! and #strip!)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What method for find and replace type function on a new copy of a string?

A

gsub (#gsub! not a copy)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Syntax for gsub for string replacement?

A

gsub(pattern,replacement)

"hello".gsub(/[aeiou]/, '*')
#=> "h*ll*"
"hello".gsub(/[eo]/, 'e' => 3, 'o' => '*')
#=> "h3ll*"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Syntax for gsub for hash?

A

gsub(pattern,hash)

"hello".gsub(/[aeiou]/, '')
#=> "hll"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Syntax for gsub for match on a block?

A

gsub(pattern){|match|,block}

"hello".gsub(/./) {|s| s.ord.to_s + ' '}
#=> "104 101 108 108 111"
17
Q

Syntax to turn all characters of a string into an array?

A

string.split(//)

To split just the words do: string.split()