Writing Ruby Iterators Collaborative Learning Exercise #8 CSC 372 March 12, 2018 Time: TBD minutes; Points: 3

Task(s)

This CLE consists of a series of activities, all of which are to be done with the same partner.

Use a single text file, cle8.txt, to hold you and your partner's accumulated work. Start it off with your two NetIDs.

Slide 185 ("A simple iterator")

  1. Add "185:" to your cle8.txt.
  2. On your slides, identify and label (1) the code that is the iterator and (2) the code that is the block. Check each others work.
  3. Pick a role, either iterator or block, with your partner being the opposite. Work through the call
    simple {|x|puts "\tx = #{x}" }
    
    statement by statement (really expression by expression), going back and forth between iterator and block as appropriate, and adding to cle8.txt the output of each of the puts calls in turn.

Slide 189 ("CLE problem: from_to")

  1. Add "189:" to your cle8.txt.
  2. Write the iterator from_to as specified on slide 189 and add it to cle8.txt.
  3. It's ok to test your code. Try this:
    $ irb -r ./from_to.rb
    >> from_to(3,4) { |x| puts x }
    3
    4
    => 2
    

Slide 193 ("A round-trip with yield, continued")

  1. As you did on slide 185, take the role of either iterator or block and work through the round-trip shown on slide 193 with your partner playing the opposite role. Perhaps cast it as a telephone conversation, where a third party starts things by calling round_trip and says "3".
  2. Add "193: Done!" to your cle8.txt.

Slide 194 ("Round trips with yield")

  1. Add "194:" to your cle8.txt.
  2. Write the iterator select as specified on slide 194 and add it to cle8.txt. The box on 195 with "What does the iterator/block interaction look like?" might provide a little insight on how to write it.
  3. It's ok to test your code.

Deliverable (per table)

A plain text file named cle8.txt with contents as described above.

Submit it on lectura with turnin:

turnin 372-cle8 cle8.txt