English 中文(简体)
preserving visibility when doing an alias_method_chain on attr_accessor in Ruby 1.9
原标题:

I m trying to port a library from ruby 1.8. I didn t write the library. I can change the API if absolutely necessary, but it does have a fair number of users I would prefer not to inconvenience.

Here s the problem simplified:

require  rubygems 
require  activesupport 

class Foo
  private
  def self.attr_accessor_with_magic(*attrs)
    attr_accessor_without_magic(*attrs)
  end

  public
  class << self
    alias_method_chain :attr_accessor, :magic
  end

  attr_accessor :bar
end

foo=Foo.new
foo.bar=17
puts foo.bar

On Ruby 1.8.7p174, this prints 17

On Ruby 1.9.1p243, I get private method ``bar= called for #<Foo:0x000000010a40f8> (NoMethodError)

Obviously, the Ruby 1.9 behaviour is correct. To better illustrate the Ruby 1.8 bug replace the public with private in the above code. Ruby 1.8 still prints 17!

Therefore I can retain "bug compatibility" with the current version of the library by writing

  private
  def self.attr_accessor_with_magic(*attrs)
    public
    attr_accessor_without_magic(*attrs)
    private
  end

Does anybody have any idea how I can do the right thing and create the appropriate visibility for the attr? On Ruby 1.8 it s going to be public no matter what I do, but is it possible to do the right thing for 1.9?

问题回答

If attr_accessor is not doing the right thing for you, you don t need to use it. As far as I know, writing attr_accessor :bar is just a quick way to define two public methods, "bar" and "bar=" that get and set the member variable @bar. So you can do that by hand instead, and add your own customizations and privacy levels to it:

class Foo
  public
  def bar
    reading_bar_magic()
    return @bar
  end

  private
  def bar=(value)
    setting_bar_magic()
    @bar=value
  end
end




相关问题
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 ...

热门标签