I want to search particular string that will be there in $stdout that I print on console using http.set_debug_output($stdout) . I want to search string "Access-Control-Allow-Origin: *\\r\\n" from $stdout and if string found want to store $stdout in text file.
$stdout
has some special restrictions in ruby. By default it is opened for reading only (obviously), so to seek/read lines that you've written, you have to replace it as a File
. This will suffice:
$stdout = File.new("stdout", "w+")
puts "some string"
Kernel#puts
will use $stdout
by default, so this sets you up perfectly. What you have to implement is checking, it can be a little tricky, but here's one way to approach it.
$stdout = File.new("stdout", "w+")
puts "some string"
old = $stdout
$stdout = IO.try_convert(STDOUT)
old.rewind
old.lines.each do |line|
puts 'contains requested data' if line == "some string\n"
end
Few things to note on the comparison, puts places \\n
automatically at the end of your string. To escape that \\n
so it matches the previous input, you have to put it in the ""
quotes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.