#! /usr/local/bin/ruby def hanoi1(n, a, b, c) return if n.zero? hanoi1(n-1, a, c, b) { |from, to| yield from, to } yield a, b hanoi1(n-1, c, b, a) { |from, to| yield from, to } end def hanoi(n) return to_enum(:hanoi, n) unless block_given? hanoi1(n, "a", "b", "c") { |from, to| yield from, to } end hanoi(7) { |from, to| puts "#{a} => #{b}" }