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?