learn_ruby_ch4_pt3 Flashcards
Range from a string include last character
Range from a string exclude last character
cite = “Act V, Scene IV”
cite[0..4] # => “Act V”
cite[0…4] # => “Act “
Use regular expression to find “horse!” at end of line
“A horse” at beginning of line index point 0
line = “A horse! a horse! my kingdom for a horse!”
line[/horse!$/] # => “horse!”
line[/^A horse/, 0] # => “A horse”
Find the index of “k”
line = “A horse! a horse! my kingdom for a horse!”
line.index(“k”) # => 21
Compare two strings in two ways
hay = "liberty" nicolay = "Liberty"
hay == nicolay # => false
hay.eql? nicolay # => false
Use the spaceship operator to determine if a string is equal to, greater than, or less than
“a” <=> “a” # => 0
“a” <=> 97.chr # => 0
“a” <=> “b” # => −1
“a” <=> “`” # => 1
Ignore case
“a” <=> “A” # => 1
“a”.casecmp “A” # => 0