On Fri, 14 Mar 2003, Ryan Pavlik wrote:

> On Fri, 14 Mar 2003 05:59:33 +0900
> "Jason M Jurkowski ;002;icsg6;" <jmj8237 / cs.rit.edu> wrote:
>
> > also, related, does anyone know if there is a good reason for the absense
> > of assertions in ruby?
>
> Assertions?  You mean like in C?
exactly.  i believe  eiffel makes a big deal about them as part of "design
by contract"
>
> > i'm guessing it is a conscious decision.
>
> Probably because there are exceptions. ;-)  You could always do:
>
>    def assert(conditional)
>       raise "Assertion failed" if not conditional
>    end
>
> But that comes out with less information in the end than if you make a
> nice, specific exception for the given case and populate it with
> useful info.
>
> Assertions (in C anyway) are mostly a quick hack because there aren't
> exceptions, anyway.
>
sometimes assertions don't check adherence to an interface.  sometimes you
like to add assertions to help you determine where a program went wrong.
you find which part of your understanding "broke first."

i've coded up a ruby "assertion" for myself as follows:

def assert(conditional)
  begin
    raise Exception, "assertion failed" if not conditional
  rescue Exception => e
    filename = e.backtrace()[1].split(/:/)[0]
    lineno = e.backtrace()[1].split(/:/)[1].to_i()
    file = File::new(filename)
    puts e.message+": "+file.readlines()[lineno-1]
    exit! 1
  end
end

maybe as i learn more ruby i'll find less of a use for it.
> > jason
>
> --
> Ryan Pavlik <rpav / users.sf.net>
>
> "Fanged death from the sea is quite routine." - 8BT
>
>