gpt4 book ai didi

Ruby 相当于 PHP 的 $_GET

转载 作者:数据小太阳 更新时间:2023-10-29 07:22:37 26 4
gpt4 key购买 nike

在 Ruby(不是 Rails 或任何其他框架)中,我至少需要什么才能轻松获取 GET 等请求数据?

我想避免必须使用 ruby​​ on rails 或任何其他框架来获取此数据,因此理想的答案不会有框架依赖性。

我当前的设置在 apache (site.com/script) 上的 cgi-bin 中有一个 ruby​​ 文件 (script)

#!/usr/bin/env ruby

# bare minimum to output content
puts "Content-type: text/html"
puts

puts "it works!" #works fine.

# how would I obtain request data like GET parameters here
# for a url like site.com/script?hi=there
# in PHP, I would $_GET['hi']

最佳答案

啊,使用 CGI 类解决了我的问题。

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/cgi/rdoc/CGI.html

require 'cgi'

cgi = CGI.new

cgi.params.each do |key, val|
puts key
puts val
end

所以 site.com/script?hi=there 会输出 hi\nthere

关于Ruby 相当于 PHP 的 $_GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12904267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com