为什么我在 ruby 类/模块错误中得到 nil

阿文

我创建了一个包装类 rclient 来使用 Excon 进行 http 调用。在我的控制器类中,我实例化并调用该类上的一个方法。在调试时,我注意到在客户端类 @client 变量的 post 方法中始终为零。

我更改了代码(参见方法 2),使用 Excon 的新实例实例化一个 @cient 变量并在同一方法中使用该变量,然后它不为空。我不确定为什么首先在方法中,我可以访问在 initialize 方法中设置的 @client 变量。有任何想法吗?

方法一

module API
   class rclient

      def initialize
        @client ||= Excon.new('http://example.com')
      end

      def post
         #error @client is nil 
         post_response = @client.Post(path:'/create', body:{date:'somedata'}); 
      end
   end
end


class my controller < ApplicationController

    def create
       req = API::rclient.new()
       req.post
    end
end

方法二

module API
   class rclient
      def post
         @client = Excon.new('http://example.com')
         post_response = @client.Post(path:'/create', body:{date:'somedata'}); 
      end
   end
end


class my controller < ApplicationController

    def create
       req = API::rclient.new()
       req.post
    end
end
由里

@client变量将被初始化为两个变体。对于所描述的场景,此代码“改编”中可能缺少 smth。这是您可以检查的稍微调整的演示代码:

# app.rb
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)

module API
  class Rclient

    def initialize
      @client ||= Excon.new('http://example.com')
    end

    def post
      post_response = @client.post(path:'/create', body: URI.encode_www_form(date: 'somedata'))
    end
  end

  class Rclient2
    def post
      @client = Excon.new('http://example.com')
      post_response = @client.post(path:'/create', body: URI.encode_www_form(date: 'somedata'))
    end
  end
end

req1 = API::Rclient.new
req2 = API::Rclient2.new

req1.post
req2.post

raise 'First implementation failed' unless req1.instance_variable_get(:@client).is_a?(Excon::Connection)
raise 'Second implementation failed' unless req2.instance_variable_get(:@client).is_a?(Excon::Connection)
# Gemfile
source 'https://rubygems.org' 
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.5'

gem 'excon'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我从 Ruby 对象的成员中得到一个 nil 值,但它不是 nil?

为什么()在Ruby中返回nil?

为什么(a,b = [nil,nil])在Ruby中为真?

为什么在RUBY DNS查询输出中附加“ nil”?

带有模块的Ruby类命名空间:为什么我用双冒号而不是模块块得到NameError?

为什么我在Ruby中收到NoMethodError?

在 Ruby 中,当初始化一个只包含 nil 的新类时,为什么在我将一个字符串铲入其中后仍然返回 nil?

为什么在Ruby on Rails中出现nil:NilClass的错误“未定义方法'each'”?

为什么我得到nil:NilClass的未定义方法“错误”?

在Ruby(1.9.3)中,为什么nil响应比较运算符`<=>`?

为什么在Ruby中未初始化的实例变量返回nil,但是未初始化的类变量引发错误?

我写了其余的api,但主体在golang中返回nil。为什么返回“ 0 {<nil> false [] <nil> false 0 <nil> 0}”

如果评估在 Ruby 中返回 nil,我如何引发异常?

Ruby中的Nil和boolean

为什么我的ruby类不转换为json?

如果我在jruby中而不是ruby中,为什么我的加密模块返回不同的值

为什么我可以在Ruby中访问数组中的常量?

为什么我无法在 Ruby 中访问哈希?

为什么我不能在 ruby 中更新哈希?

为什么我得到“ nil”不是与ActiveModel兼容的对象。它必须实现:to_partial_path错误吗?

为什么在Windows与Ubuntu中,用于true,false和nil的Ruby object_ids似乎有所不同?

当我消除所有nil_s时,为什么在展开Optional值时却得到nil?

Ruby uri模块解析产生nil

什么是“ nil”条件的Ruby优雅方式?

为什么错误与nil不匹配?走

如果我没有在nil上执行代码,为什么Rails会引发错误

我不断收到此错误“在展开可选值时意外发现nil”,为什么?

为什么在Lua中“ not nil”返回true?

为什么要在init()中检查nil