New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating Struct class with length attribute #1724

Closed
lomereiter opened this Issue May 16, 2012 · 3 comments

Comments

Projects
None yet
3 participants
@lomereiter

lomereiter commented May 16, 2012

The following code produces incorrect results:

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

In MRI and JRuby this prints 42 as expected.

@IPGlider

This comment has been minimized.

Show comment
Hide comment
@IPGlider

IPGlider May 16, 2012

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 = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
puts joe.length

Test1 = Struct.new(:length)
t1 = Test1.new(42)
puts t1.length

Test2 = Struct.new(:length, :other)
t2 = Test2.new(42, "other")
puts t2.length

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

http://www.ruby-doc.org/core-1.9.3/Struct.html#method-i-length

Member

IPGlider commented May 16, 2012

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 = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
puts joe.length

Test1 = Struct.new(:length)
t1 = Test1.new(42)
puts t1.length

Test2 = Struct.new(:length, :other)
t2 = Test2.new(42, "other")
puts t2.length

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

http://www.ruby-doc.org/core-1.9.3/Struct.html#method-i-length

@lomereiter

This comment has been minimized.

Show comment
Hide comment
@lomereiter

lomereiter May 16, 2012

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.)

lomereiter commented May 16, 2012

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 added a commit to carlosgaldino/rubinius that referenced this issue May 16, 2012

@carlosgaldino

This comment has been minimized.

Show comment
Hide comment
@carlosgaldino

carlosgaldino May 16, 2012

Member

Thanks for your report. It's fixed now.

Member

carlosgaldino commented May 16, 2012

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