I have found following statement in date.rb:
def <=> (other)
case other
when Numeric; return @ajd <=> other
when Date; return @ajd <=> other.ajd
end
raise TypeError, 'expected numeric or date’
end
So I can’t compare a date with nil, because I get an error. Instead I
have to write something like date.instance_of? NilClass, which is
something unnecessary complicated.
So I can’t compare a date with nil, because I get an error. Instead I
have to write something like date.instance_of? NilClass, which is
something unnecessary complicated.
Why is this implemented this way?
Why do you want to compare date with nil, when nil <=> date raises
NoMethodError? Although it is better complied to other part of Ruby,
if it returns nil instead of raising exception.
So I can’t compare a date with nil, because I get an error. Instead I
have to write something like date.instance_of? NilClass, which is
something unnecessary complicated.
I have found following statement in date.rb:
def <=> (other)
case other
when Numeric; return @ajd <=> other
when Date; return @ajd <=> other.ajd
end
raise TypeError, ‘expected numeric or date’
end
Try newer version.
def <=> (other)
case other
when Numeric; return @ajd <=> other
when Date; return @ajd <=> other.ajd
end
nil
end
Because in this specific case I don’t know whether this variable is
uninitialized (nil) or is a Date or something else. I write a database
library and I check if the values I am inserting are not null. If they
aren’t, the only data type I have problem with is Date, because I can’t
compare it with nil.
···
On Mon, 3 Mar 2003 11:14:21 +0900, Yukihiro Matsumoto wrote:
So I can’t compare a date with nil, because I get an error. Instead I
have to write something like date.instance_of? NilClass, which is
something unnecessary complicated.
Why is this implemented this way?
Why do you want to compare date with nil, when nil <=> date raises
NoMethodError? Although it is better complied to other part of Ruby,
if it returns nil instead of raising exception.
On Tue, 4 Mar 2003 01:07:03 +0900, Matt Armstrong wrote:
So I can’t compare a date with nil, because I get an error. Instead I
have to write something like date.instance_of? NilClass, which is
something unnecessary complicated.
Try:
I think that if foo <=> nil returned nil, it would have a number of
important and possibly undesirable knock-on effects. For example, what would
'sort' do when spaceship returns nil? Would foo < nil also end up returning
nil? What would happen if you tried to compare two other non-comparable
classes, e.g. 3 <=> true ?
You might find that one of the following patterns achieves what you want
without too much difficultly:
<do something> unless foo.nil? # shorter than 'foo.instance_of? NilClass'
foo ||= SomeDefaultValue # only assigns if foo==nil or false
Or you could use begin...rescue...end around the code section.
Regards,
Brian.
···
On Mon, Mar 03, 2003 at 06:15:18PM +0900, Child wrote:
On Mon, 3 Mar 2003 11:14:21 +0900, Yukihiro Matsumoto wrote:
>>So I can't compare a date with nil, because I get an error. Instead I
>>have to write something like date.instance_of? NilClass, which is
>>something unnecessary complicated.
>>
>>Why is this implemented this way?
>
> Why do you want to compare date with nil, when nil <=> date raises
> NoMethodError? Although it is better complied to other part of Ruby,
> if it returns nil instead of raising exception.
Because in this specific case I don't know whether this variable is
uninitialized (nil) or is a Date or something else. I write a database
library and I check if the values I am inserting are not null. If they
aren't, the only data type I have problem with is Date, because I can't
compare it with nil.
statements = <<-sql
insert into
mytable
values ( #{user}, #{comment}, #{date or Time.now} )
sql
conn.exec statements
but better still
create table mytable
(
user text,
comment text,
date timestamp default now()
)
since this will handle ‘nil’ values from ruby, perl, c, sql-command prompt,
etc.
IMHO: if there is the notion of a ‘default’ date for your schema, it should be
represented in the schema - not ruby code. if there is not, the TypeError
raised should halt the program so date <=> nil need not be changed. of
course, this is not always feasible but i thought i’d mention it…
-a
···
On Mon, 3 Mar 2003, Brian Candler wrote:
You might find that one of the following patterns achieves what you want
without too much difficultly:
unless foo.nil? # shorter than ‘foo.instance_of? NilClass’
foo ||= SomeDefaultValue # only assigns if foo==nil or false
Or you could use begin…rescue…end around the code section.
–
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================