English 中文(简体)
页: 1
原标题:Ruby: having callbacks on attr objects

基本上,我不禁要问,如何在废墟上对物体进行 call击,这样,当物体在任何地方发生变化时,我可以自动引发其他变化:

(EDIT:我以我自己的榜样混淆了!) 不是好的信号...... 由于Empoxy是一种URI物体,它有自己的方法,使用自己的方法改变URI物体,没有叫我自己的proxy= 方法,并更新@httpc.

class MyClass
  attr_reader :proxy
  def proxy=(string_proxy = "")
    begin
      @proxy = URI.parse("http://"+((string_proxy.empty?) ? ENV[ HTTP_PROXY ] : string_proxy))
      @http = Net::HTTP::Proxy.new(@proxy.host,@proxy.port)
    rescue
      @http = Net::HTTP
    end
  end
end

m = MyClass.new
m.proxy = "myproxy.com:8080"
p m.proxy
# => <URI: @host="myproxy.com" @port=8080>

m.proxy.host =  otherproxy.com 
p m.proxy
# => <URI: @host="otherproxy.com" @port=8080>
# But accessing a website with @http.get( http://google.com ) will still travel through myproxy.com as the @http object hasn t been changed when m.proxy.host was.
最佳回答

我亲眼看到这一点!

# Unobtrusive modifications to the Class class.
class Class
  # Pass a block to attr_reader and the block will be evaluated in the context of the class instance before
  # the instance variable is returned.
  def attr_reader(*params,&block)
    if block_given?
      params.each do |sym|
        # Create the reader method
        define_method(sym) do
          # Force the block to execute before we…
         self.instance_eval(&block)
          # … return the instance variable
          self.instance_variable_get("@#{sym}")
        end
      end
    else # Keep the original function of attr_reader
      params.each do |sym|
        attr sym
      end
    end
  end
end

如果您补充说,该编码在哪一处分发attr_reader。 方法,如果你现在做以下工作:

attr_reader :special_attr { p "This happens before I give you @special_attr" }

它给你@special_attr前触发碎块。 例如,在从互联网下载属性的课堂上,它就能够使用。 如果您界定了如下方法:get_details,该方法适用于所有检索,并设置@details_retrvortrue <>>>。 那么,你可以这样界定:

attr_reader :name, :address, :blah { get_details if @details_retrieved.nil? }
问题回答

页: 1 没有对<代码>empt?作出答复。 因此,@ 载于Net:HTTP,如救助条款。

这与呼吁/设定无关。 如果使用积极的支持,你应修改你的守则,以做你想要的东西(例如,电话string_proxy.blank?)。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签