Readline, input separator and Ruby

In Ruby, I look for data to read until I get to the delimiter or end of the file.

I found it possible by overriding $/ or $INPUT_RECORD_SEPARATOR in my delimiter. However, with all the “features” in the Ruby language, it seems that hackey is changing the global meaning for this. In addition, readline is used to use the delimiter, not counting it in what is returned.

Is there another way to "read to" when consuming a delimiter that does not involve getting char values ​​on char in a loop?

+4
source share
2 answers

Basically, all readline style methods from IO accept a separator string as an optional parameter:

 >> s = StringIO.new('hello from string') => #<StringIO:0x52bf34> >> s.readline ' ' => "hello " >> s.readline ' ' => "from " >> s.readline ' ' => "string" 
+3
source

You can specify a record separator as an argument to get, for example:

 while line = gets("\r") puts line end 
+1
source

Source: https://habr.com/ru/post/1264586/


All Articles