Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

ruby - Why can't I use a class instance variable inside a singleton class definition?

I'm trying to set an instance variable inside a singleton class and I can't get it to work.

Here's a simplified version of the problem:

class MyClass
  class << self
    attr :my_attr

    @my_attr = {}

    def my_method (x, y)
      (@my_attr[x] ||= []) << y
    end
  end
end

MyClass.my_method(1, 2)
# => NoMethodError: undefined method `[]' for nil:NilClass

Here's the original code sample:

class Mic
  class Req < Rack::Request; end
  class Res < Rack::Response; end

  class << self
    attr :routes

    @routes = {}

    def call(env)
      dup.call!(env)
    end

    def call!(env)
      (@app||=new).call(env)
    end

    def get(path, opts={}, &blk)
      puts @routes.inspect # nil
      route 'GET', path, opts, &blk
    end

    def route(type, path, opts, &blk)
      (@routes[type]||=[]) << {type: type, path: path, opts: opts, blk: blk}
    end
  end

  def call(env)
    @env = env
    @req = Req.new(env)
    @res = Res.new

    @res.finish
  end
end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, abbreviated code, but what you probably want is to avoid accessing the instance variable as much as possible.

class Mic
  class << self
     def routes
       @routes ||= {}
     end

     def method_which_acccess_routes
       routes[:this] = :that
     end
   end

   def instance_method_access_routes
      Mic.routes[:the_other] = :nope
   end
 end

You can modify routes in place this way without an accessor, but if you need to completely overwrite it, you'll need an attr_writer method for routes as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...