小编kor*_*tes的帖子

Ruby:在方法中调用'gets'方法保存Nil而不是Input

好的,这是在终端上运行的Dungeon Text Adventure项目中.

当用户说他想"向北"时,它会拆分字符串:第一个字检查方法,第二个字检查参数.这发生在其他地方,我复制粘贴了2个方法,给我以下问题:

当它调用go(北)并且north不是有效连接时,它显示选项并再次询问用户方向,用户在那一刻输入的输入由于某种原因存储为Nil.

为什么??我也尝试过STDIN.gets.chomp.downcase!并有相同的Nil结果

这是代码:

    def find_room_in_direction(direction)

##-> if room connects with other on that direction, returns connection

        if find_room_in_dungeon(@player.location).connections.include?(direction.to_sym)
            return find_room_in_dungeon(@player.location).connections[direction.to_sym]

##-> if direction connection is not found, 
##-> show possible connections & return trigger to ask again inside go()

         elsif !find_room_in_dungeon(@player.location).connections.include?(direction.to_sym)
             puts "I don't see any #{direction}..."
             puts "This room only connects #{(find_room_in_dungeon(@player.location)).connections.keys.join(', ')}"
             puts "Where should we go?"

             return :redo
         end
         end


    def go(direction)

        current_direction = @player.location
        new_direction = find_room_in_direction(direction)

##-> if REDO trigger received, …
Run Code Online (Sandbox Code Playgroud)

ruby terminal gets ruby-on-rails input

1
推荐指数
1
解决办法
213
查看次数

标签 统计

gets ×1

input ×1

ruby ×1

ruby-on-rails ×1

terminal ×1