Method referring to a pulled in packaged's method moves out of scope when put inside an objects method

后端 未结 2 416
抹茶落季
抹茶落季 2021-01-26 09:31

When I put a method that refers to a pulled in package inside another method it leaves scope and fails.

What is the proper way to do this. I tried playing with the \'se

2条回答
  •  隐瞒了意图╮
    2021-01-26 10:07

    1. The mistake in the code is that @sandbox is an attribute of the class. The value would be initialized when an object of the class is created. Writing the initialization in the class will have no effect. @Maxim has explained this in his answer.

    2. For the second code, when the interpreter runs through the code, it would execute it once. But that code cannot run more than once.

    The code should be,

    require 'package that has 'accounts''
    
    
    class Name
    
        def initialize
          @sandbox = #working API connection
        end
    
        def get_account
            @sandbox.accounts do |resp|         #This is where error is
              resp.each do |account|
    
                if account.name == "John"
                    name = account.name
                end
    
              end
            end
        end
    
    
    end
    
    
    new = Name.new
    p new.get_account
    

提交回复
热议问题