learn_ruby_ch4_pt3 Flashcards

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

Range from a string include last character

Range from a string exclude last character

A

cite = “Act V, Scene IV”

cite[0..4] # => “Act V”

cite[0…4] # => “Act “

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

Use regular expression to find “horse!” at end of line

“A horse” at beginning of line index point 0

A

line = “A horse! a horse! my kingdom for a horse!”

line[/horse!$/] # => “horse!”

line[/^A horse/, 0] # => “A horse”

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

Find the index of “k”

A

line = “A horse! a horse! my kingdom for a horse!”

line.index(“k”) # => 21

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

Compare two strings in two ways

A
hay = "liberty"
nicolay = "Liberty"

hay == nicolay # => false

hay.eql? nicolay # => false

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

Use the spaceship operator to determine if a string is equal to, greater than, or less than

A

“a” <=> “a” # => 0
“a” <=> 97.chr # => 0
“a” <=> “b” # => −1
“a” <=> “`” # => 1

Ignore case
“a” <=> “A” # => 1
“a”.casecmp “A” # => 0

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