1 / 15

CIT 383: Administrative Scripting

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

tarak
Download Presentation

CIT 383: Administrative Scripting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings and Variables CIT 383: Administrative Scripting CIT 383: Administrative Scripting

  2. CIT 383: Administrative Scripting Topics • Single-quoted strings. • Double-quoted strings. • Choose your own quotes. • Characters. • String operators.

  3. 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.’

  4. 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.”

  5. String Escapes CIT 383: Administrative Scripting

  6. 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”

  7. 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.|

  8. 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

  9. 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

  10. 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’

  11. 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 ...

  12. 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’

  13. 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

  14. 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”

  15. 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.

More Related