Class: Rack::HanoiApp

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/hanoi.rb

Instance Method Summary collapse

Constructor Details

#initialize(root, height, from = :A, to = :C, via = :B) ⇒ HanoiApp

Returns a new instance of HanoiApp.



20
21
22
# File 'lib/rack/hanoi.rb', line 20

def initialize(root, height, from=:A, to=:C, via=:B)
  @root, @height, @from, @to, @via = root, height, from, to, via
end

Instance Method Details

#call(env) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/hanoi.rb', line 24

def call(env)
  env["rack.hanoi"] ||= ""
  logger = env["rack.logger"] || ::Logger.new(STDERR)
  if @height < 1
    return Rack::Response.new do |res|
      res["Content-Type"] = "text/plain"
      res.body << with_lines(env["rack.hanoi"])
    end.finish
  else
    HanoiApp.new(false, @height - 1, @from, @via, @to).call(env)
    logger.info "Disc Moved #{@from} to #{@to}"
    env["rack.hanoi"] << "Disc Moved #{@from} to #{@to}\n"
    HanoiApp.new(false, @height - 1, @via, @to, @from).call(env)
  end
end