Ruby Control Structures Venkat Subramaniam svenkat@cs.uh.edu 1 - - PDF document

ruby control structures
SMART_READER_LITE
LIVE PREVIEW

Ruby Control Structures Venkat Subramaniam svenkat@cs.uh.edu 1 - - PDF document

Ruby Control Structures Venkat Subramaniam svenkat@cs.uh.edu 1 Statements Statements dont need ; You write one statement per line (like you normally do anyways) If you want to put multiple statements on a line, you may use


slide-1
SLIDE 1

1

Venkat Subramaniam – svenkat@cs.uh.edu

Ruby Control Structures

2

Venkat Subramaniam – svenkat@cs.uh.edu

Statements

  • Statements don’t need ;
  • You write one statement per line (like

you normally do anyways)

  • If you want to put multiple statements on

a line, you may use the optional ;

slide-2
SLIDE 2

3

Venkat Subramaniam – svenkat@cs.uh.edu

Conditions

  • if statement does not need { }
  • Short forms

4

Venkat Subramaniam – svenkat@cs.uh.edu

Looping

  • while loop is similar to what you’re used

to

More concise form

slide-3
SLIDE 3

5

Venkat Subramaniam – svenkat@cs.uh.edu

For loops

There are some other cool ways as well, but we’ll have to wait for that until we learn about blocks

6

Venkat Subramaniam – svenkat@cs.uh.edu

Notice the Range

  • We used 1..10 and 1..val
  • Range is a first class citizen in Ruby
  • You can create range objects
  • It comes in handy in various situations…

we’ll see these when discussing collections