From: Robert Kedoin Date: 2001-05-17T01:44:06+09:00 Subject: [ruby-talk:15299] Q: Should initializers validate arguments ? I'm new to Ruby and I came across something today and I was wondering if it's a bug or a philosophical issue. In Ruby 1.6.3, it is possible to create a Range object with two Time object's as start and end. However, since Time does not respond to #succ, when you try to send most methods to the Range you fail since #each cannot work. I would have expected that I would have gotten some sort of exception when I created the Range. Similarly, it is possible to create a Range with a start and and end that are of completely different classes. Again this doesn't generate any error at creation time, but fails to do anything meaningful later. Is it a design decision not to check the fitness of these arguments and wait until later to find out if the Range is valid. Or are they bugs ? Below is some code that demonstrates what I'm talking about. Robert Kedoin p.s I've since found the Date class which did what I wanted, but I was still confused about the behavior I was seeing... -Rob --- cut here --- #!/usr/bin/env ruby # # experiment with using Time in a range require 'date' thisYear = Time.now.year now = Time.now today = Time.local(now.year,now.month,now.day) # Create a range of Time's for the month of May puts "Creating a range of Time" may = Time.local(thisYear,5,1)..Time.local(thisYear,5,30) #-> I would have expected an error here since Time doesn't support #succ begin # puts "range length is #{may.length}" puts may.include?(today) rescue NameError $stderr.puts "Range operation failed:" + $! end # Simlarly it is possible to create a range with two completely unrelated # classes puts "\nCreating a range from Date to Fixnum" may = Date.new(thisYear,5,1)..5 #-> I would have expected an error here since although Date and Fixnum support # #succ, it probably isn't meaningful to count from one to the other puts may.to_a puts "Range length is #{may.length}" # Interestingly enough, reversing the Fixnum and Date *does* generate an # error when creating the Range puts "\nCreating a range from Fixnum to Date" may = 5..Date.new(thisYear,5,1)