gpt4 book ai didi

ruby - 如何有条件地选择两种方法之一来接收相同的 block

转载 作者:太空宇宙 更新时间:2023-11-03 17:53:37 24 4
gpt4 key购买 nike

我正在尝试更改依赖于变量 req 的代码的过程,您如何在此处看到:

@res = @conn.post do |request| if req == 'post'
@res = @conn.get do |request| if req == 'get'

问题是这似乎引发了一个错误:

stack.rb:89: syntax error, unexpected end-of-input, expecting keyword_end
user2.send_csr

我的问题是,我必须更改什么才能避免此问题?如果您需要有关我的代码的更多信息:

def send(req,ww,text1=nil,text2=nil)
@conn = Faraday.new 'https://zombo.de/rest', :ssl => {:verify => false}
@conn.basic_auth(@username,@password)
@res = @conn.post do |request| if req == 'post'
@res = @conn.get do |request| if req == 'get'
request.url ww
request.headers['Content-Type'] = text1 unless text1 == nil
request.body = text2 unless text2 == nil
end
puts @res.body
end

def send_csr
send('post','csr','text/plain',"#{File.read(@csr[0..-5])}")
end

user2.send_csr

最佳答案

如果您稍微扩展一下代码会怎样?添加一些格式并更改进入两个 block 的内容?

def send(req, ww, text1=nil, text2=nil)
@conn = Faraday.new 'https://zombo.de/rest', :ssl => {:verify => false}

@conn.basic_auth(@username,@password)

@res = @conn.post { |request| handle_request(request) } if req == 'post'
@res = @conn.get { |request| handle_request(request) } if req == 'get'

@res.body
end

def handle_request request
request.url ww
request.headers['Content-Type'] = text1 unless text1 == nil
request.body = text2 unless text2 == nil
request
end

def send_csr
send('post','csr','text/plain',"#{File.read(@csr[0..-5])}")
end

user2.send_csr

关于ruby - 如何有条件地选择两种方法之一来接收相同的 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19045918/

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