From: Morton Goldberg Date: 2006-11-08T03:44:31+09:00 Subject: Re: Struct.new and symbol with question mark On Nov 6, 2006, at 11:58 PM, Suraj Kurapati wrote: > 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 > => # > >>> x.foo > => nil > >>> x.bar? > NoMethodError: undefined method `bar?' for # :bar?=nil> > from (irb):5 > from :0 > > I was expecting Klass to have the #foo, #foo=, #bar?, and #bar= > methods. It doesn't work because :bar? isn't a valid symbol literal. The follow does work: #! /usr/bin/env ruby -w Baz = Struct.new(:foo, :'bar?') z = Baz.new(42, false) z # => # z.bar? # => false Regards, Morton