首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby portal.new父母

Ruby portal.new父母
EN

Stack Overflow用户
提问于 2017-11-13 19:18:54
回答 1查看 53关注 0票数 1

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

代码语言:javascript
复制
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,我对我的程序进行了“测试”,并收到了以下结果,最后只发生了错误:

代码语言:javascript
复制
[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变量:

代码语言:javascript
复制
[2] pry(main)> new.response
=> #<Test::Portal:0x00007fbaca96e1c8
 @klass=Test::Response,
 @parent=
  #<Test::Client:0x00007fbaca975540.....>

由于正确设置了@response变量并正确继承了该变量,所以我将尝试并从new =Test::Client.new中调用Test::Client.new:

代码语言:javascript
复制
[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::门户行:

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

method_missing函数中:

代码语言:javascript
复制
def method_missing method, *args, &block
    @klass.public_send method, @parent, *args, &block
end

如何允许创建的Test::Client对象在Test::Response中使用get方法,并在初始化方法中使用Test::Client的@conn和conn方法设置@conn。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-30 18:00:12

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

Via: engineersmnky 11月13日20:29

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47271911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档