How to Retrieve Variables from Webrick Servers Starting in Ruby

Asked 2 years ago, Updated 2 years ago, 46 views

I'm a beginner at Ruby.While running the Webrick server in Ruby, I would like to see the variables that are being substituted by the server from a separate file [test.rb], but every time I look at the variables from a different file, the server opens and I can't see them.

Using global, instance, or constant did not change the results.
[test.rb] also tried to use load or eval file.read instead of require, but the server still opens and the result is not output until the server is stopped.The server must be open all the time, so you can't stop it every other time.

I think the same thing will happen not only in the server but also in the case of retrieving the variables of the state of the moment at a specific timing of loop processing. Is this not possible in the first place?It would be very helpful if you could let me know.

[server.rb]

require 'webrick'
require "json"

s = WEBrick::HTTPServer.new({
  Port: 8000,
})

post=Array.new(0)

s.mount_proc('/') do | req, res|

  post.push(req.body)#req.body is hash

end

Signal.trap('INT') {s.shutdown}
s.start

[test.rb]

require'./server.rb'
put post

ruby

2022-09-30 18:38

1 Answers

I don't know what you were referring to, but both the server and the client side are almost completely strange.In particular, the client only require the actual server code, but the client does not have the necessary code at all.

If it's just "client retrieves server data", it's simply like this.

#Server side
s.mount_proc('/') do | req, res|
  res.body=JSON.generate(post)
end

# client side
require 'open-uri'

pJSON.parse(open('http://server:8080').read)


2022-09-30 18:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.