Attr_readers, attr_writers and visibility differences

Hi all,

While getting our application to run on Jruby, I’ve found a difference
between MRI and JRuby. The code at http://pastie.org/949819 does
different things:

MRI (1.8.7)
$ ruby visibility.rb
5
10

Jruby(1.5.0RC3)
$ ruby visibility.rb
5
visibility.rb:19:in a': private methodb=’ called for
#<MyClass:0x6483e5f1 @b=5> (NoMethodError)
from visibility.rb:24

Is this a bug?

// Wouter de Bie


The Delta Projects
Wouter de Bie, Systemutvecklare, [email protected]
mobil: +46 (0)720 180 77 7, telefon: +46 (0)8-667 76 90, fax: +46
(0)8-651 76 00
adress: banérgatan 10, 115 23 stockholm sweden
e-post: [email protected] internet: www.deltaprojects.se /
www.conto.se
Confidentiality statement
The information in this e-mail and any attachments is confidential. It
is the property of the Sender and is only intended for the use of the
specified recipient(s). If you are not one of the specified recipients
please notify the sender immediately. Distribution of an incorrectly
received message may be unlawful.

Looks like you missed “:b” as the argument to attr_reader and
attr_writer in
your example script. Also, it appears that neither activesupport nor
rubygems are needed.

I do see the same behavior as you. But witness:

$ ruby -v a.rb
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
a.rb:7: warning: private attribute?
5
10
$ jruby -v a.rb
jruby 1.6.0.dev (ruby 1.8.7 patchlevel 249) (2010-05-06 09cccce) (Java
HotSpot™ Client VM 1.6.0_17) [i386-java]
5
a.rb:19:in a': private methodb=’ called for #<MyClass:0x4ed904 @b=5>
(NoMethodError)
from a.rb:24
$ rvm use ruby-head
Using ruby head
$ ruby -v a.rb
ruby 1.9.2dev (2010-04-21 trunk 27426) [x86_64-darwin10.2.0]
a.rb:7: warning: private attribute?
5
a.rb:19:in a': private methodb=’ called for
#<MyClass:0x00000100867d28
@b=5> (NoMethodError)
from a.rb:24:in `’

Ruby 1.8.7 does warn about a private attribute, while both JRuby and
Ruby
1.9.2 raise an error. While it’s certainly a valid argument that JRuby
isn’t
behaving the same as MRI, this is an edge case that I’m not sure is
worth
fixing unless you can give us evidence of a well-used library that
breaks
due to this behavior.

/Nick

On Fri, May 7, 2010 at 4:37 AM, Wouter de Bie <

Hi Nick,

The :b was copied and pasted wrong, but the real world example had it.
I’ve
already changed the code to fix the issue, which isn’t in a commonly
used
lib.

Thanks for checking!

// Wouter

On 7 mei 2010, at 18:19, Nick S. [email protected] wrote:

Looks like you missed “:b” as the argument to attr_reader and
attr_writer in
your example script. Also, it appears that neither activesupport nor
rubygems are needed.

I do see the same behavior as you. But witness:

$ ruby -v a.rb
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
a.rb:7: warning: private attribute?
5
10
$ jruby -v a.rb
jruby 1.6.0.dev (ruby 1.8.7 patchlevel 249) (2010-05-06 09cccce) (Java
HotSpot™ Client VM 1.6.0_17) [i386-java]
5
a.rb:19:in a': private methodb=’ called for #<MyClass:0x4ed904 @b=5>
(NoMethodError)
from a.rb:24
$ rvm use ruby-head
Using ruby head
$ ruby -v a.rb
ruby 1.9.2dev (2010-04-21 trunk 27426) [x86_64-darwin10.2.0]
a.rb:7: warning: private attribute?
5
a.rb:19:in a': private methodb=’ called for
#<MyClass:0x00000100867d28
@b=5> (NoMethodError)
from a.rb:24:in `’

Ruby 1.8.7 does warn about a private attribute, while both JRuby and
Ruby
1.9.2 raise an error. While it’s certainly a valid argument that JRuby
isn’t
behaving the same as MRI, this is an edge case that I’m not sure is
worth
fixing unless you can give us evidence of a well-used library that
breaks
due to this behavior.

/Nick

On Fri, May 7, 2010 at 4:37 AM, Wouter de Bie <

File this as a bug. It’s a pretty minor thing, but for an “op
assignment”
like this we’re doing the actual assignment as though it were always
happening outside the class. Because it’s private, this causes an error.

Oddly enough, we do it correctly for simple “attribute assignment” as in
self.b=, so if you did self.b = b + 5 it would work fine. They just
parse
differently, and the += version is not handling visibility right.

On Fri, May 7, 2010 at 1:45 PM, Wouter de Bie <

Hi!

Yes, that’s the way I fixed it :wink:

// Wouter

On 7 mei 2010, at 22:04, Charles Oliver N. [email protected]
wrote:

File this as a bug. It’s a pretty minor thing, but for an “op
assignment”
like this we’re doing the actual assignment as though it were always
happening outside the class. Because it’s private, this causes an error.

Oddly enough, we do it correctly for simple “attribute assignment” as in
self.b=, so if you did self.b = b + 5 it would work fine. They just
parse
differently, and the += version is not handling visibility right.

On Fri, May 7, 2010 at 1:45 PM, Wouter de Bie <