# ch9Mar13 - 9.2.4 Ruby Blocks a block is a sequence of...

This preview shows pages 1–10. Sign up to view the full content.

1 9.2.4 Ruby Blocks a block is a sequence of statements delimited by braces or a do-end pair (as usual, but. ..) block must start on the line of a method call a block can have formal parameters, specified between vertical bars: |num| the block is called with a yield statement in the method: yield first sends the actual parameter first to the formal parameter num

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 # A method to compute and yield Fibonacci numbers # up to a limit def fibonacci(last) first, second = 1, 1 while first <= last yield first first, second = second, first + second end end # Call fibonacci with a block to display the numbers puts "Fibonacci numbers less than 100 are:" fibonacci(100) {|num| print num, " "} puts # Output a newline # Call Fibonnacci again to sum the values sum = 0 fibonacci(100) {|num| sum += num} puts "Sum of the Fibonacci numbers less than 100 is: #{sum}" sends to block the actual param block with formal param num parallel assignment convert sum to string
3 \$ ruby test6.rb Fibonacci numbers less than 100 are: 1 1 2 3 5 8 13 21 34 55 89 Sum of the Fibonacci numbers less than 100 is: 232 Output from the two Ruby blocks

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Ruby blocks: What is the output? def test7(last) first = 1 while first <= last yield first first = first + 1 end end test7(5) {|num| print num, " "} puts # Output a newline sum = 0 test7(5) {|num| sum += num} puts "Sum : #{sum}"
5 Ruby blocks: answer ruby test7.rb 1 2 3 4 5 Sum : 15

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 9.2.5 Procedures and Functions both are collections of statements that define parametrized computations functions return a result void functions behave like procedures Ada and Fortran provide both C-based languages provide only functions (and/or methods) Ada, Python, Ruby, C++, C# can overload operators
7 9.3 Design Issues for Subprograms Static or dynamic allocation of local variables? Can subprogram definitions be nested? What parameter passing methods are used? Type checking of actual vs. formal params? Reference environment of passed subprog.s? Overloading of subprograms? Generic subprograms?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 9.4 Local Referencing Environment Local variables = ones defined inside subprog Scope usu. limited to body of the subprogram static: bound to storage at before program execution begins and remain bound to same memory cells until program execution ends. stack-dynamic: bound to storage when subprog. begins execution and unbound when execution terminates.
Stack-dynamic local variables Advantages flexibility needed for recursion shared space Disadvantages allocation & deallocation time indirect access no history, e.g. as needed in pseudorandom no. generator

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/29/2010 for the course COMPUTER S cps615 taught by Professor Pro during the Spring '10 term at Randolph College.

### Page1 / 32

ch9Mar13 - 9.2.4 Ruby Blocks a block is a sequence of...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online