Why isn’t the Struct class smart enough to detect a question mark at the
end of a member name and create the appropriate #member= and #member?
methods?
Klass = Struct.new :foo, :bar?
=> Klass
x = Klass.new
=> #<struct Klass foo=nil, :bar?=nil>
x.foo
=> nil
x.bar?
NoMethodError: undefined method `bar?’ for #<struct Klass foo=nil,
:bar?=nil>
from (irb):5
from :0
I was expecting Klass to have the #foo, #foo=, #bar?, and #bar= methods.
Why isn’t the Struct class smart enough to detect a question mark at the
end of a member name and create the appropriate #member= and #member?
methods?
It could have been made smart enough, but certain people decided that
sort of convenience was “incorrect programming”.
See ruby-core:5796 and following.
Regards,
Dan
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.