Strings and variables
Download
1 / 15

CIT 383: Administrative Scripting - PowerPoint PPT Presentation


  • 65 Views
  • Uploaded on

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 \

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CIT 383: Administrative Scripting' - tarak


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Strings and variables

Strings and Variables

CIT 383: Administrative Scripting

CIT 383: Administrative Scripting


Topics

CIT 383: Administrative Scripting

Topics

  • Single-quoted strings.

  • Double-quoted strings.

  • Choose your own quotes.

  • Characters.

  • String operators.


Single quoted strings

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


Double quoted strings

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
String Escapes

CIT 383: Administrative Scripting


Interpolation

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”


Choose your own quotes

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


Here documents

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


Here documents1

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


Character literals

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’


String operators

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


Logical operators

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’


Accessing characters

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


Substrings

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”


References

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.


ad