ruby 中输入字符串的判断方法

来源:互联网 发布:男士双肩包 知乎 编辑:程序博客网 时间:2024/06/05 11:03
require 'test/unit'class Test_First < Test::Unit::TestCase  def test_gets    line = ""    while 1      line = gets      puts line      if line[/exit/]        puts line        break              end    end    assert_equal line,"exit"  endend


gets输入的字符串带有回车符号,及"\n",所以要判读可以使用[/exit/]或者就用 line.strip 去掉后面的回车符号。

原创粉丝点击