我正在通过Zed Shaw学习Ruby Ruby the Hard Way,我希望看看我是否能够获得第一笔额外学分。他请你解释一下第一个房间是如何工作的。除了.call()如何工作之外,我想我理解其中的大部分内容。这就是我想出来的,有人能告诉我,如果我得到了它,或者我是不是基地了吗?
class Game
def initialize(start)
@quips = [
"You died. You kinda suck at this.",
"Nice job, you died...jackass.",
"Such a luser.",
"I have a small puppy that's better at this."
]
@start = start
end
def prompt()
print"> "
end
def play()
next_room = @start
# While next_room = @start (only in the beginning), room then gets
# defined as room = method(next_room)
# a method is a set of expressions that returns a value.
# with methods, you can organize code into subroutines that
# can be easily invoked from other areas of their program.
# so, this method sets room to next_room, and next_room is then
# set to room.call()
# room.call() is then set by what is returned at the end of the code.
#at every room, it returns either death or
# the name of the next room/function
while true
puts "\n---------"
room = method(next_room)
next_room = room.call()
end
end
def death()
puts @quips[rand(@quips.length())]
Process.exit(1)
end
def central_corridor()
puts "The Gothons of Planet Percal #25 have invaded your ship and destroyed"
puts "your entire crew. You are the last surviving member and your last"
puts "mission is to get the neutron destruct bomb from the Weapons Armory,"
puts "put it in the bridge, and blow the ship up after getting into an "
puts "escape pod."
puts "\n"
puts "You're running down the central corridor to the Weapons Armory when"
puts "a Gothon jumps our, red scaly skin, dark grimy teeth, and evil clown costume"
puts "flowing around his hate filled body. He's blocking the door to the"
puts "Armory and about to pull a weapon to blast you."
prompt()
action = gets.chomp()
if action == "shoot!"
puts "Quick on the draw you yank our yoru blaster and fire it at the Gothon."
puts "His clown costume is flowing and moving around his body, which throws"
puts "off your aim. Your laser hits his costume but misses him entirely. This"
puts "completely ruins his brand new costume his mother bought him, which"
puts "makes him fly into an insane rage and blas you repeatedly in the face until"
puts "you are dead. Then he eats you."
return :death
elsif action == "dodge!"
puts "Like a world class boxer you dodge, weave, slipe and slide right"
puts "as the Gothon's blaster cranks a laser past your head."
puts "In the middle of your artful dodge your foot slips and you"
puts "bang your head on the metal wall and pass out."
puts "You wake up shortly after only to die as the Gothon stomps on"
puts "your head and eats you."
return :death
elsif action == "tell a joke"
puts "Luck for you they made you learn Gothon incults in teh academy."
puts "You tell the one Gothon joke you know:"
puts "Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fgvf nebhaq gur ubhfr."
puts "The GOthon stops, tries not to laugh, then busts our laughing and can't move."
puts "While he's laughing you run up and shoot him square in the head"
puts "putting him down, then jump through the Weapon Armory door."
return :laser_weapon_armory
else
puts "DOES NOT COMPUTE!"
return :central_corridor
end
end
答案 0 :(得分:0)
它执行传递给方法的文字,就好像它们被称为函数
一样这是一个例子
def test
"Hello World"
end
p method(:test).call
#=>"Hello World"
p method("test").call
#=>"Hello World"