Project

General

Profile

Actions

Bug #19416

closed

Inconsistent behaviour for Struct.new without any member_names

Added by herwin (Herwin W) almost 2 years ago. Updated over 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux-gnu]
[ruby-core:112222]

Description

When I simply declare a Struct without any arguments, I get an error:

irb(main):001:0> Struct.new
(irb):1:in `new': wrong number of arguments (given 0, expected 1+) (ArgumentError)
        from (irb):1:in `<main>'                        

But Struct has an option to pass a class name as the first argument, which will create the struct as a constant in the Struct namespace. If this argument is given, there is no ArgumentError

irb(main):002:0> Struct.new('Foo')
=> Struct::Foo

This results in a rather pointless class

irb(main):003:0> Struct::Foo.new(1)
(irb):3:in `initialize': struct size differs (ArgumentError)
        from (irb):3:in `new'                                
irb(main):004:0> Struct::Foo.new
=> #<struct Struct::Foo>

This behaviour is not documented in the Struct class, but I would guess this is not how it is intended to be.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0