【问题标题】:Ruby portal.new parent, klassRuby portal.new 父级,类
【发布时间】:2017-11-13 19:18:54
【问题描述】:

所以我只是想在一个模块中创建一个客户端类,然后在客户端初始化方法中使用一个门户类来创建一个响应类。我正在尝试创建一种允许 Client 类访问 Response 类方法和 instance_variables 的方法。下面是我为测试所有这些而创建的粗略的 ruby​​ 文件和模块/类架构:

module Test
    class Client

        attr_accessor :response

        def initialize
            conn
            @response = Portal.new self, Response
        end

        def conn
            @conn = Faraday.new(:url => 'http://api.openweathermap.org') do |faraday|
                faraday.request  :url_encoded             # form-encode POST params
                faraday.response :logger                  # log requests to STDOUT
                faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
            end
        end
    end

    class Portal
        def initialize parent, klass
            @parent = parent
            @klass = klass
        end

        def method_missing method, *args, &block
            @klass.public_send method, @parent, *args, &block
        end
    end

    class Response

        attr_accessor :conn, :res

        def initialize client, params
            @client = client
            conn
        end

        def conn
            @conn
        end

        def get
            @res = conn.get do |req|
                    req.url '/data/2.5/weather'
                    req.params['q'] = @city_country_state
                    req.params['APPID'] = @consumer_api_key
                    req.params['units'] = @units
                end
        end
    end
end

使用 binding.pry 我“测试”了我的程序并收到以下内容,最后只出现错误:

[1] pry(main)> new = Test::Client.new
=> #<Test::Client:0x00007fbaca975540
 @conn=
  #<Faraday::Connection:0x00007fbaca975310.....>
@response=
  #<Test::Portal:0x00007fbaca96e1c8
   @klass=Test::Response,
   @parent=#<Test::Client:0x00007fbaca975540 ...>>>

在创建并看到 Portal 类在里面创建了一个 Test::Response 类后,我检查了 Test::Client @response 变量:

[2] pry(main)> new.response
=> #<Test::Portal:0x00007fbaca96e1c8
 @klass=Test::Response,
 @parent=
  #<Test::Client:0x00007fbaca975540.....>

由于 @response 变量设置正确并正确继承,我开始尝试从 new = Test::Client.new: 中调用 Test::Response.get:

[3] pry(main)> new.response.get
NoMethodError: undefined method `get' for Test::Response:Class
from test.rb:47:in `public_send'

test.rb 中的第 47 行指的是 Test::Portal 行:

@klass.public_send method, @parent, *args, &amp;block

method_missing 函数内部:

def method_missing method, *args, &block
    @klass.public_send method, @parent, *args, &block
end

如何让创建的Test::Client对象在Test::Response中使用get方法,在initialize方法中使用Test::Client的@conn和conn方法设置@conn。

【问题讨论】:

  • Response 实例变量 @conn 永远不会设置,并且将永远是 nil 我认为这是一个问题。同样,现在您正在尝试调用Response.get,但get 是一个实例方法,因此可以尝试将@response 初始化为@response = Portal.new self, Response.new(conn,{})

标签: ruby


【解决方案1】:

响应实例变量@conn 永远不会设置并且永远为零我认为这是一个问题。同样,现在您正在尝试调用 Response.get 但 get 是一个实例方法,因此可以尝试将 @response 初始化为 @response = Portal.new self, Response.new(conn,{})

通过:engineersmnky 11 月 13 日 20:29

【讨论】:

    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2014-07-24
    • 2012-10-18
    • 2018-06-27
    相关资源
    最近更新 更多