dwilbank
dwilbank

Reputation: 2520

Why return a symbol of a method instead of just calling the method? (Ruby the Hard Way Ex43)

As i study this lesson, I see the following blocks and methods.

while true
  puts "\n--------"
  room = method(next_room)
  next_room = room.call() #calls central_corridor
end

def death()
  puts "you die"
  Process.exit(1)
end

def central_corridor()
  puts "He's about to pull a weapon to blast you."
  prompt(); action = gets.chomp()

  if action == "shoot!"
    puts "Your laser misses him entirely."
    return :death

So returning :death clearly launches the death() method, but why is this better than simply calling death() outright ?

Could not this game go through its entire range of "rooms" just by "calling" other room methods?

Is the purpose simply to teach us about the method method?

Thanks

Upvotes: 0

Views: 45

Answers (1)

Darshan Rivka Whittle
Darshan Rivka Whittle

Reputation: 34031

The program is designed so that each "room" has a method, and the protocol when calling a room method is to do whatever is appropriate for that room, then return the symbol of the next room. This way the instance of Game can keep track of which room is current, which room is next, and call the appropriate method. It's a reasonable design decision that may seem like overkill at this small scale, but it would make sense if the game were to grow.

Upvotes: 1

Related Questions