ruby control flow n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Ruby Control Flow PowerPoint Presentation
Download Presentation
Ruby Control Flow

Loading in 2 Seconds...

play fullscreen
1 / 14

Ruby Control Flow - PowerPoint PPT Presentation


  • 100 Views
  • Uploaded on

Ruby Control Flow. and other languages… . Standard Control Flow. Selection Statements Iterative Statements Unconditional Branching Not covered Guarded Commands. Conditionals. if expr code end # newline after expr # false or nil, don’t execute, else do # no () around expr

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 'Ruby Control Flow' - tareq


Download Now 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
ruby control flow

Ruby Control Flow

and other languages…

standard control flow
Standard Control Flow
  • Selection Statements
  • Iterative Statements
    • Unconditional Branching
  • Not covered
    • Guarded Commands
conditionals
Conditionals

if expr

code

end

# newline after expr

# false or nil, don’t execute, else do

# no () around expr

# end required even if only one statement in code

if expr then code end

if expr

code

else

code

end

if expr1

code

elsif expr2

code2

else

code

end

conditional return value
Conditional return value
  • Return value is last expression executed OR nil

x=5

# topic: return values

name = if x==1 then "Cyndi"

else "Nancy"

end

puts name

expression modifier
Expression Modifier

Perl also has this syntax

if expr then code end

equivalent to:

code if expr

Best practice: use when condition is trivial or normally true

other conditionals
Other conditionals

unless expr

code

end

code unless expr

tax = case income

when 0..7550

income * 0.1

when 7550..30650

income * 0.15

when 3065..50000

income * 0.25

else

income * 0.9

end

also other forms of case, not covered

Compare to switch. Consider language readabilty.

iteration
Iteration

while expr do

code

end

code while expr

until expr do

code

end

code until expr

Pascal had repeat… until.

more iteration
More iteration

for var in collection do

code

end

# do is optional, can use newline

hash.each do |key, value|

puts “#{key} => #{value}”

end

iterators
Iterators
  • Integer.times, Enumerable.each, map and Integer.upto
  • make use of yield (next slide)
  • Examples

2.times { puts “OK” }

array.each {|x| puts x }

[5,10,15].map { |x| x*x*x }

factorial = 1

2.upto(20) { |x| factorial *= x}

yield
yield

yield temporarily returns control from iterator to calling method

QUICK EXERCISE:

Trace the code on the next two slides. Turn in for class participation.

yield1
yield

You are in the method

You are in the block

You are again back to the method

You are in the block

from: http://www.tutorialspoint.com/ruby/ruby_blocks.htm

  • yield temporarily returns control from iterator to calling method

def test

puts "You are in the method"

yield

puts "You are again back to the method"

yield

end

test {puts "You are in the block"}

  • method must be invoked with a block (otherwise, what would you yield to?)
another example same site
another example, same site

You are in the block 5

You are in the method test

You are in the block 100

Java = pull (caller controls iteration), Ruby = push (iterator controls iteration)

def test

yield 5

puts "You are in the method test"

yield 100

end

test {|i| puts "You are in the block #{i}"}

unconditional branching
Unconditional Branching

Transfers execution control to a specified place in the program

Represented one of the most heated debates in 1960’s and 1970’s

Well-known mechanism: goto statement

Major concern: Readability

Some languages do not support goto statement (e.g., Module-2, Java, Python, Ruby)

C# offers goto statement (can be used in switch statements)

Loop exit statements are restricted and somewhat camouflaged goto’s

topic summary
Topic Summary
  • Language Concepts
    • selection/conditionals
    • return value
    • iteration
    • unconditional branching
  • Ruby
    • if/elsif
    • conditional return value
    • expression modifier
    • unless
    • while/until
    • for
    • each
    • yield