From: Joel VanderWerf Date: 2003-04-09T07:52:34+09:00 Subject: Re: The "!" and "?" characters. Chris Pine wrote: > ----- Original Message ----- > From: "Daniel Carrera" > > This is cool. But it'd be cooler if I could write 'def !' and '5.!'. > But 'def !' causes a parse error. > > Now my question: > Does anyone know why 'def !' causes a parse error? > ---------------------------- > > Because it's interpreting `!' as `not', I think. You can usually get around > these problems by sending symbols, but `:!' gives a parse error. You can > usually get around *those* problems by interning a string: > > irb(main):001:0> class Integer > irb(main):002:1> define_method ('!'.intern) {self * > (self-1).send('!'.intern)} > irb(main):003:1> end > => # > irb(main):004:0> 5.send ('!'.intern) > Segmentation fault > > Oops. > > Hmmm... > > Chris > With a test for the self==1 case, it works... class Integer define_method (:'!') do self == 1 ? 1 : self * (self-1).send(:'!') end end p(5.send(:'!')) # ==> 120 but I dont think there's any way to call #! except with #send, and that ain't pretty.