Creating Struct class with length attribute #1724

lomereiter opened this Issue May 16, 2012 · 3 comments

3 participants


The following code produces incorrect results:

S = :length
s = 42
puts s.length #=> 1

In MRI and JRuby this prints 42 as expected.

Rubinius member

I think this is a different behavior between Rubinius and both MRI and JRuby. #length is supposed to return the number of instance variables in the Struct object.

Customer =, :address, :zip)
joe ="Joe Smith", "123 Maple, Anytown NC", 12345)
puts joe.length

Test1 =
t1 =
puts t1.length

Test2 =, :other)
t2 =, "other")
puts t2.length

In my understanding the expected results are 3, 1 and 2.


Is supposed by default, but here I explicitly request it to define accessor for field 'length'. Anyway, this is used in BioRuby, and its maintainers are inclined to think it is bug in Rubinius: bioruby/bioruby#52 (comment)

(Personally, I would prefer warning or even exception to be thrown in case of such name clash, because it can lead to very subtle bugs, especially when code contains heavy metaprogramming.)

@carlosgaldino carlosgaldino added a commit to carlosgaldino/rubinius that referenced this issue May 16, 2012
@carlosgaldino carlosgaldino Add spec for Struct#length when accessor method is defined
See #1724
@carlosgaldino carlosgaldino added a commit that closed this issue May 16, 2012
@carlosgaldino carlosgaldino Don't specialize Struct methods after defining accessors
In MRI if we define a Struct with attributes that have the same name
that other default methods that ship with any Struct instance, the
default methods will be overriden. For example:

S = :length
s = 42
s.length # => 42

This commit makes Rubinius compliant with the MRI behavior by defining
all accessor methods after specializing. This way any Struct attribute
with the same name of a default method will override the default method.

Fixes #1724
Rubinius member

Thanks for your report. It's fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment