In ruby, I want to write a code that reads from the standard input one line at a time, and ends it after reading a blank line, the end of the file, and nil, but it does not work as intended.
I wrote the following code,
def input_line() line=gets() return line.chomp! end def input_lines() lines= while(line=input_line) if line==nil or line=='\n'# or EOF break end lines.push(line) end end
First of all, if you enter from the keyboard, even if you type a blank line, it does not end, and you need to type Ctrl+Z to stop typing.
In the terminal of vs code, from the file,
cat data.txt | .\ruby_small_utensils.rb
If i input as, the operation will stop in the input waiting state.
In the first place, since the newline character is removed by the input_line method, while (line=input_line) becomes false if there is an empty line, I initially thought that I should not need if or less, but since it does not work, add if or less I did, but it still doesn't work.
Furthermore, if you include "or EOF" which is currently commented out, it will be said to be an uninitialized constant.
Answer # 1
You should check the specifications of the method you use.
nilWill be at EOF. If it is a blank line
"\n"Only line breaks.
while(line=input_line)So in the loop
if line==nilThe check is meaningless.
chomp!Therefore, if it is a blank line, it will be an empty string.
chomp!I don't know whether I wrote it without understanding the meaning or forgot to write it, but the judgment of the blank line is
"\n"Has a different meaning, the latter is a newline character, while the former is a two-character "backslash n".
getsResult without checking the value
chomp!So at the time of EOF
nil.chomp!Is an error.
chompLet's also check the difference between.
- ruby - i want to know the reason for implementing it in a class method or module when solving an exercise
- ruby on rails - i want to write a test code that matches the rspec value
- [ruby on rails] how to write when getting a model nested in multiple stages with includes
- ruby - binary data conversion method
- ruby - no method error rails
- ruby - i'm using the errors method, but i don't get an error message
- ruby on rails: undefined method ʻuser'for nil: about nomethoderror of nilclass
- ruby - undefined method `save'for nil: nilclass cannot be resolved
- ruby - i want to write css that displays buttons by tapping
- ruby - improvement of nomethoderror undefined method `paginate'
- [ruby] "attr_accessor" i don't understand this method
- ruby on rails - [rails] i want to make it a helper method
- ruby - i want to perform standard output with multiple lines and write/read multiple files
- swift - it is a method that is not used, but when you have to write a process because it conforms to protocol
- ruby - nomethoderror: undefined method `new'for: uglifier: symbol error on heroku deployment
- ruby on rails - [rails] i want to use each statement in a helper method
- ruby data acquisition how to write ruby
- ruby on rails - i want to write a rails tutorial test in rspec
- ruby - cannot save data by save method
- ruby - △△ how to write haml
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- xcode - pod install [!] no `podfile 'found in the project directory
- dart - flutter: the instance member'stars' can't be accessed in an initializer error