How to use “gets” and “gets.chomp” in Ruby

前端 未结 6 1111
一整个雨季
一整个雨季 2020-12-01 06:29

I learned that gets creates a new line and asks the user to input something, and gets.chomp does the same thing except that it does not create a ne

6条回答
  •  一整个雨季
    2020-12-01 06:57

    chomp is the method to remove trailing new line character i.e. '\n' from the the string. whenever "gets" is use to take i/p from user it appends new line character i.e.'\n' in the end of the string.So to remove '\n' from the string 'chomp' is used.

    str = "Hello ruby\n"

    str = str.chomp

    puts str

    o/p

    "Hello ruby"

提交回复
热议问题