From: Jim Freeze Date: 2005-09-14T03:18:56+09:00 Subject: Re: yet another simple command-line option parser On 9/13/05, Eric Mahurin wrote: > Thanks for the input Jim. Comments below. I'm also putting > this in the RCR comments. > > No problem. Fixnum inherits from Integer. Do you care whether > Fixnum.from_s returns a Fixnum or a Bignum? It could return > either just like many of the other Fixnum instance methods. I was just thinking it would be strange for me to write Fixnum.from_s(...) and get back a BigNum. The natural thing to do is to have it return a BigNum, but that is not what was requested. The symmetric thing to do (see below) is to have it raise an exception, but that would have little use and be quite annoying. > > 2. Do we use to_i or Integer(#) - Integer raises and to_i > > does not > > 3. Do we use Float or to_f - Float raises and to_f does not > > Good point. This RCR should to specify this. I would think it > best if an exception occur if the full string doesn't parse the > the target type. I'll change the implementation to use the > methods that raise exceptions. In the two cases above I think an exception should be raised. > require 'parsedate' > class MyTime < Time > def self.from_s(s) > gm(*ParseDate.parsedate(s)) > end > end > > and then make the default be a MyTime instead of a Time. Yes, that would work, and be a pain. My thought is that if #from_s was integrated into Ruby, we wouldn't have this problem. The parsing functionality would be built into the Time class. But even so, there will always be cases like those above, but with different classes. Is there a way to handle them in an aesthetically pleasing way? -- Jim Freeze