Download Ruby Control Flow

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
and other languages…
 Selection
Statements
 Iterative Statements

Unconditional Branching
 Not
covered
• Guarded Commands
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
 Return
value is last expression executed
OR nil
x=5
# topic: return values
name = if x==1 then "Cyndi"
else "Nancy"
end
puts name
if expr then code end
 equivalent to:
code if expr
 Best
practice: use when condition is
trivial or normally true
Perl also has this syntax
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.
while expr do
code
end
until expr do
code
end
code while expr
code until expr
Pascal had repeat… until.
for var in collection do
code
end
# do is optional, can use newline
hash.each do |key, value|
puts “#{key} => #{value}”
end
 Integer.times
 Enumerable.each
 Enumerable.Map
 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
temporarily returns control from
iterator to calling method
 QUICK EXERCISE:
• Trace the code on the next two slides. Turn in for
class participation.
• Format flexible… draw arrows etc, just show you
understand
• DISCUSS: when/why might this be useful? We’ll
discuss as a class
 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
You are in the method
end
You are in the block
You are again back to the method
You are in the block
test {puts "You are in the block"}

method must be invoked with a block (otherwise, what
would you yield to?)
from: http://www.tutorialspoint.com/ruby/ruby_blocks.htm
def test
yield 5
puts "You are in the method test"
yield 100
end
test {|i| puts "You are in the block #{i}"}
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)
 When/why
might yield be useful?

Silly code example.
• Write code similar to the second yield trace (slide 12) that will display the
modulo 15 of all numbers from 100 downto 90.
• Your yield expression should provide two values (look up the syntax).
• Hint: in the output below, what is the literal test that repeats? What values
change?

Example output:
100 modulo 15 is 10
99 modulo 15 is 9
98 modulo 15 is 8
97 modulo 15 is 7
96 modulo 15 is 6
95 modulo 15 is 5
94 modulo 15 is 4
93 modulo 15 is 3
92 modulo 15 is 2
91 modulo 15 is 1

Nothing to submit.
 What’s
really going on here? Language
design: importance of blocks
 Read:
http://www.artima.com/intv/closures.html
 Language
Concepts
 Ruby
• selection/conditionals
• if/elsif
• return value
• conditional return
• iteration
•
•
•
•
•
•
value
expression modifier
unless
while/until
for
each
yield