艰苦学习Ruby ex.43额外学分
我正在努力学习Zed Shaw的《艰苦学习Ruby》,我想看看我是否能获得第一个额外学分。他让你解释归还第一个房间的工作原理。除了.call()的工作原理外,我想我了解它的大部分内容。这就是我想到的,有人能告诉我我是得到了还是离开了基地吗艰苦学习Ruby ex.43额外学分,ruby,Ruby,我正在努力学习Zed Shaw的《艰苦学习Ruby》,我想看看我是否能获得第一个额外学分。他让你解释归还第一个房间的工作原理。除了.call()的工作原理外,我想我了解它的大部分内容。这就是我想到的,有人能告诉我我是得到了还是离开了基地吗 class Game def initialize(start) @quips = [ "You died. You kinda suck at this.", "Nice job, you died...jackas
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
它执行传递给方法的文本,就好像它们是作为函数调用的一样 这里有一个例子
def test
"Hello World"
end
p method(:test).call
#=>"Hello World"
p method("test").call
#=>"Hello World"