150 likes | 249 Views
Strings and Variables. CIT 383: Administrative Scripting. Topics. Single-quoted strings. Double-quoted strings. Choose your own quotes. Characters. String operators. Single-quoted Strings. Create strings using single quotes. ‘Hello ruby’ Escape ‘ using
E N D
Strings and Variables CIT 383: Administrative Scripting CIT 383: Administrative Scripting
CIT 383: Administrative Scripting Topics • Single-quoted strings. • Double-quoted strings. • Choose your own quotes. • Characters. • String operators.
CIT 383: Administrative Scripting Single-quoted Strings Create strings using single quotes. • ‘Hello ruby’ Escape ‘ using \ • ‘O\’Reilly published Learning Ruby.’ • ‘A \ is just itself in the middle.’ • ‘This string ends with one backslash.\\’ • ‘You can also have multi-line strings using \ to escape the newline character.’
CIT 383: Administrative Scripting Double-quoted Strings Create strings using double quotes. • “Hello Ruby” Double quoted strings have more escapes • “Hello \”Rubyist\”” • “A multi-\nline string.” • “\tString indented by one tab.” • “No need to use backslash to escape newlines in double quoted strings.”
String Escapes CIT 383: Administrative Scripting
CIT 383: Administrative Scripting Interpolation Include result of code in double-quoted string • “1 + 1 == #{1+1}” • x = 2*3.1415926 • “360 degrees == #{x} radians”
CIT 383: Administrative Scripting Choose your own Quotes If your string has a lot of ‘ or “ in it, you have to do a lot of escaping so ... Ruby allows you to choose your own quotes • %q acts like single-quoted string • %Q acts like double-quoted string • Character after q or Q is the delimiter. • Initial and final delimiters are identical unless you’re using one of a matched pair: (,[,{,< match ),],},> respectively. Examples • %q(No need to worry about escaping ‘ here) • %Q|Or for escaping “ in this string.| • %Q|But you do have to escape \| here.|
CIT 383: Administrative Scripting Here Documents For long string literals, any chosen delimiter may be used within the string, so Ruby can delimit text using arbitrary strings like bash. document = <<HERE <html><head> <title>Here Document!</title> </head><body> “A quoted body isn’t normal.” </body></html> HERE
CIT 383: Administrative Scripting Here Documents Behave like double-quoted strings • String interpolation • Escape characters Single-quoted here documents: document = <<‘EOD’ You can use #{1+1} to escape ruby code, and you can use \t as backslash and t, as they don’t do anything special here. EOD
CIT 383: Administrative Scripting Character Literals Single characters denoted by a ? prefix • ?a is the character a • ?” is the double-quote character • ?\t is the tab character Not the same as a single character string • ?a != ‘a’
CIT 383: Administrative Scripting String Operators Concatenation • “Hello” + “ “ + “Ruby” == “Hello Ruby” Converting numbers to strings • version = 1.9 • “Hello Ruby “ + version.to_s == “Hello Ruby 1.9” • “Hello Ruby #{version}” Multiplication • ellipsis = ‘.’*3 # Evaluates to ...
CIT 383: Administrative Scripting Logical Operators Equality ‘Hello’ == ‘Hello’ Inequality ‘Hello’ != ‘hello’ Less Than ‘a’ <= ‘b’ Less Than or Equal To ‘a’ <= ‘a’ Greater Than ‘baz’ > ‘bar’ Greater Than or Equal To ‘baz’ >= ‘baz’
CIT 383: Administrative Scripting Accessing Characters Use index to access individual characters x = “Hello” x[0] == ?H x[1] == ?e Negative numbers index from the end x[-1] == ?o x[-2] == ?l Use index to modify string, -1 index special x[0] = ?M # changes x to Mello x[-1] = “” # changes to Mell
CIT 383: Administrative Scripting Substrings Use double index to access substrings x = “Hello” x[0,2] == “He” x[-2,2] == “lo” x[0,0] == “” # For all strings x[0,10] == “Hello” x[0,-1] == nil # Negative lens ret nil Modify string by assigning to index x[0,2] = “Ma” x[-2,2] = “ow” x == “Mallow” x[2,2] = “” x == “Maow”
CIT 383: Administrative Scripting References • Michael Fitzgerald, Learning Ruby, O’Reilly, 2008. • David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. • Hal Fulton, The Ruby Way, 2nd edition, Addison-Wesley, 2007. • Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2nd edition, Pragmatic Programmers, 2005.