This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Getting the Parts of a String You Want
CodeExpectedActual
s = 'My kingdom for a string!'
s.slice(3,7)
"kingdom" "kingdom"
s[3,7]
"kingdom" "kingdom"
s[0,3]
"My " "My "
s[11, 5]
"for a" "for a"
s[11, 17]
"for a string!" "for a string!"
s[/.ing/]
"king" "king"
s[/str.*/]
"string!" "string!"
s.slice(3)
107 107
s[3]
107 107
107.chr
"k" "k"
s.slice(3,1)
"k" "k"
s[3,1]
"k" "k"
s.slice(-7,3)
"str" "str"
s[-7,6]
"string" "string"
s[15...s.length]
"a string!" "a string!"