Check assertion in Ruby

Hi, everybody. I would like to use an assert like function to test the number of rows returned by a query. Something like Eiffel's

check result.num_rows = 1

What's the best way to do that in Ruby?

TIA
Ed

···

--
Edgardo Hames
Consultor Informático
Vates S.A. Ingeniería de Software

ehames@vates.com
Tel: +54 +351 +5709800/01/02 int. 121
Av. Colón 778 - 4° piso. Córdoba

def check
    raise "check failed" unless yield
  end

  check { result.num_rows == 1 }

Regards,

  Michael

···

On Sat, Jun 05, 2004 at 03:34:47AM +0900, Edgardo Hames wrote:

Hi, everybody. I would like to use an assert like function to test the
number of rows returned by a query. Something like Eiffel's

check result.num_rows = 1

What's the best way to do that in Ruby?

Received: Sat, 5 Jun 2004 03:34:47 +0900
And lo, Edgardo wrote:

Hi, everybody. I would like to use an assert like function to test the
number of rows returned by a query. Something like Eiffel's

check result.num_rows = 1

What's the best way to do that in Ruby?

result.num_rows == 1 or raise "num_rows is not 1"

is equivalent to

raise "num_rows is not 1" unless result.num_rows == 1

The 'or/and' method is idiomatic, while the 'if/unless' is more readable.

- Greg

Michael Neumann wrote:

···

On Sat, Jun 05, 2004 at 03:34:47AM +0900, Edgardo Hames wrote:

Hi, everybody. I would like to use an assert like function to test the number of rows returned by a query. Something like Eiffel's

check result.num_rows = 1

What's the best way to do that in Ruby?

  def check
    raise "check failed" unless yield
  end

  check { result.num_rows == 1 }

Awesome! I love Ruby!

--
Edgardo Hames
Consultor Informático
Vates S.A. Ingeniería de Software

ehames@vates.com
Tel: +54 +351 +5709800/01/02 int. 121
Av. Colón 778 - 4° piso. Córdoba

if DEBUG
  def assert
    raise "assertion failure" unless yield
  end
else
  def assert; end
end

There is some overhead when not in DEBUG mode, but that
is the best you can do in Ruby as far as I know.

Yours,

Jean-Hugues

···

At 03:41 05/06/2004 +0900, you wrote:

On Sat, Jun 05, 2004 at 03:34:47AM +0900, Edgardo Hames wrote:

Hi, everybody. I would like to use an assert like function to test the
number of rows returned by a query. Something like Eiffel's

check result.num_rows = 1

What's the best way to do that in Ruby?

def check
   raise "check failed" unless yield
end

check { result.num_rows == 1 }

Regards,

Michael

-------------------------------------------------------------------------
Web: @jhr is virteal, virtually real
Phone: +33 (0) 4 92 27 74 17

Hi --

> Hi, everybody. I would like to use an assert like function to test the
> number of rows returned by a query. Something like Eiffel's
>
> check result.num_rows = 1
>
> What's the best way to do that in Ruby?

  def check
    raise "check failed" unless yield
  end

  check { result.num_rows == 1 }

I guess you could even do:

  def check(a)
    raise "check failed" unless a
  end

  check result.num_rows == 1

though of course that wouldn't scale if the assertion got more complex
syntactically. (I'm not sure it's any better anyway, but I was trying
to match Edgardo's sample syntax as closely as I could.)

David

···

On Sat, 5 Jun 2004, Michael Neumann wrote:

On Sat, Jun 05, 2004 at 03:34:47AM +0900, Edgardo Hames wrote:

--
David A. Black
dblack@wobblini.net

Hi --

···

On Sat, 5 Jun 2004, Gregory Millam wrote:

Received: Sat, 5 Jun 2004 03:34:47 +0900
And lo, Edgardo wrote:

> Hi, everybody. I would like to use an assert like function to test the
> number of rows returned by a query. Something like Eiffel's
>
> check result.num_rows = 1
>
> What's the best way to do that in Ruby?
>

result.num_rows == 1 or raise "num_rows is not 1"

is equivalent to

raise "num_rows is not 1" unless result.num_rows == 1

The 'or/and' method is idiomatic, while the 'if/unless' is more readable.

I didn't know idioms had to be unreadable :-))

David

--
David A. Black
dblack@wobblini.net

That's the best I know of that you could do in pure ruby.

If you were to write an extension then, if DEBUG is false you could have
it modify the code in place whenever assert is called. I doubt the
performance gain would be significant enough to make it worthwhile,
though.

Paul

···

On Sat, Jun 05, 2004 at 05:51:44AM +0900, Jean-Hugues ROBERT wrote:

if DEBUG
  def assert
    raise "assertion failure" unless yield
  end
else
  def assert; end
end

There is some overhead when not in DEBUG mode, but that
is the best you can do in Ruby as far as I know.

"David A. Black" <dblack@wobblini.net> schrieb im Newsbeitrag
news:Pine.LNX.4.44.0406041648060.6593-100000@wobblini...

Hi --

> > Hi, everybody. I would like to use an assert like function to test the
> > number of rows returned by a query. Something like Eiffel's
> >
> > check result.num_rows = 1
> >
> > What's the best way to do that in Ruby?
>
> def check
> raise "check failed" unless yield
> end
>
>
> check { result.num_rows == 1 }

I guess you could even do:

  def check(a)
    raise "check failed" unless a
  end

  check result.num_rows == 1

though of course that wouldn't scale if the assertion got more complex
syntactically. (I'm not sure it's any better anyway, but I was trying
to match Edgardo's sample syntax as closely as I could.)

IMHO the block form is better since the block is only evaluated if DEBUG is
switched on, while in your case the expression always evaluate.

Regards

    robert

···

On Sat, 5 Jun 2004, Michael Neumann wrote:
> On Sat, Jun 05, 2004 at 03:34:47AM +0900, Edgardo Hames wrote:

Or even better combining both approaches:

   def check(a=nil, &block)
     raise "check failed" unless (block ? block.call : a)
   end

Wouldn't that make sense to add to module Kernel? Or maybe named as
assert?

I quite often need an assert method...

Regards,

  Michael

···

On Sat, Jun 05, 2004 at 08:50:22AM +0900, David A. Black wrote:

Hi --

On Sat, 5 Jun 2004, Michael Neumann wrote:

> On Sat, Jun 05, 2004 at 03:34:47AM +0900, Edgardo Hames wrote:
> > Hi, everybody. I would like to use an assert like function to test the
> > number of rows returned by a query. Something like Eiffel's
> >
> > check result.num_rows = 1
> >
> > What's the best way to do that in Ruby?
>
> def check
> raise "check failed" unless yield
> end
>
>
> check { result.num_rows == 1 }

I guess you could even do:

  def check(a)
    raise "check failed" unless a
  end

Paul Brannan wrote:

···

On Sat, Jun 05, 2004 at 05:51:44AM +0900, Jean-Hugues ROBERT wrote:

if DEBUG
def assert
   raise "assertion failure" unless yield
end
else
def assert; end
end

There is some overhead when not in DEBUG mode, but that
is the best you can do in Ruby as far as I know.

That's the best I know of that you could do in pure ruby.

Is anyone using cpp (C preprocessor) along with Ruby? I've always wanted to use it, but my laziness prevails everytime :slight_smile:

--
dave

Michael Neumann wrote:

Or even better combining both approaches:

   def check(a=nil, &block)
     raise "check failed" unless (block ? block.call : a)
   end

Wouldn't that make sense to add to module Kernel? Or maybe named as
assert?

I don't like unnecessary object construction, such as that Proc-wrapping.

def check(a=nil)
   raise "check failed" unless (block_given? ? yield(a) : a)
end

I'd also yield the given argument if both a block and an argument are supplied. It is nice to avoid an extra assignment line when doing larger checks:

check( my.insanely.long.chained.method.call(:with,:lots,:of,*arguments) ){|i| (i > 64 && i < 91) || (i > 96 && i < 123) }

IMHO

···

--
(\[ Kent Dahl ]/)_ _~_ _____[ Kent Dahl - Kent Dahl ]_____/~
  ))\_student_/(( \__d L b__/ Master of Science in Technology )
( \__\_õ|õ_/__/ ) _) Industrial economics and technology management (
  \____/_ö_\____/ (____engineering.discipline_=_Computer::Technology___)

it is not possible to use the assert-stuff in test::unit ?

···

il Sat, 5 Jun 2004 18:37:43 +0900, Michael Neumann <mneumann@ntecs.de> ha scritto::

Wouldn't that make sense to add to module Kernel? Or maybe named as
assert?

I quite often need an assert method...

"David Garamond" <lists@zara.6.isreserved.com> schrieb im Newsbeitrag
news:40C18065.8000704@zara.6.isreserved.com...

Paul Brannan wrote:
>
>>if DEBUG
>> def assert
>> raise "assertion failure" unless yield
>> end
>>else
>> def assert; end
>>end
>>
>>There is some overhead when not in DEBUG mode, but that
>>is the best you can do in Ruby as far as I know.
>
> That's the best I know of that you could do in pure ruby.

Is anyone using cpp (C preprocessor) along with Ruby? I've always wanted
to use it, but my laziness prevails everytime :slight_smile:

Aaaargh! Even Bjarne would be glad of getting rid of this fossile and you
walk around freely and try to get people to use it! Get hold of him, arrest
him, put him in chains...

:-))

Seriously, who needs CPP when there is Ruby?

def load_special(file)
  if DEBUG
    load file
  else
    Object.new.instance_eval File.read(file).gsub(/^\s*assert\b/m, '#' )
  end
end

Regards

    robert

···

> On Sat, Jun 05, 2004 at 05:51:44AM +0900, Jean-Hugues ROBERT wrote:

Sure it is...

  require 'test/unit'
  include Test::Unit::Assertions

Thanks :slight_smile:

Regards,

  Michael

···

On Sat, Jun 05, 2004 at 07:28:45PM +0900, gabriele renzi wrote:

il Sat, 5 Jun 2004 18:37:43 +0900, Michael Neumann <mneumann@ntecs.de> > ha scritto::

>
>Wouldn't that make sense to add to module Kernel? Or maybe named as
>assert?
>
>I quite often need an assert method...

it is not possible to use the assert-stuff in test::unit ?

Robert Klemme wrote:

Is anyone using cpp (C preprocessor) along with Ruby? I've always wanted
to use it, but my laziness prevails everytime :slight_smile:

Aaaargh! Even Bjarne would be glad of getting rid of this fossile and you
walk around freely and try to get people to use it! Get hold of him, arrest
him, put him in chains...

:-))

Seriously, who needs CPP when there is Ruby?

def load_special(file)
  if DEBUG
    load file
  else
    Object.new.instance_eval File.read(file).gsub(/^\s*assert\b/m, '#' )
  end
end

I don't think preprocessing is obsolete as a concept. We can still do cool/useful stuffs with preprocessing, e.g. embedding license text, templates, obfuscation, adding/removing comments, removing debugging code, etc.

I still think preprocessing interesting because Ruby hasn't done dead-code-removal optimization like Python does (if __debug__: ..., IIRC, haven't touched Python in ages). And your example won't work with precompiled Ruby bytecode in the future :slight_smile:

···

--
dave

Cool !
Now. What about multi-lines asserts ?
If that helps I may concede a end-of-xxx marker.

Yours,

Jean-Hugues

···

At 18:23 05/06/2004 +0900, you wrote:

"David Garamond" <lists@zara.6.isreserved.com> schrieb im Newsbeitrag
news:40C18065.8000704@zara.6.isreserved.com...

Paul Brannan wrote:
> On Sat, Jun 05, 2004 at 05:51:44AM +0900, Jean-Hugues ROBERT wrote:
>
>>if DEBUG
>> def assert
>> raise "assertion failure" unless yield
>> end
>>else
>> def assert; end
>>end
>>
>>There is some overhead when not in DEBUG mode, but that
>>is the best you can do in Ruby as far as I know.
>
> That's the best I know of that you could do in pure ruby.

Is anyone using cpp (C preprocessor) along with Ruby? I've always wanted
to use it, but my laziness prevails everytime :slight_smile:

Aaaargh! Even Bjarne would be glad of getting rid of this fossile and you
walk around freely and try to get people to use it! Get hold of him, arrest
him, put him in chains...

:-))

Seriously, who needs CPP when there is Ruby?

def load_special(file)
if DEBUG
   load file
else
   Object.new.instance_eval File.read(file).gsub(/^\s*assert\b/m, '#' )
end
end
Regards
   robert

-------------------------------------------------------------------------
Web: http://hdl.handle.net/1030.37/1.1
Phone: +33 (0) 4 92 27 74 17

"Jean-Hugues ROBERT" <jean_hugues_robert@yahoo.com> schrieb im Newsbeitrag
news:6.0.1.1.0.20040605205551.01e159b0@pop.mail.yahoo.com...

>Seriously, who needs CPP when there is Ruby?
>
>def load_special(file)
> if DEBUG
> load file
> else
> Object.new.instance_eval File.read(file).gsub(/^\s*assert\b/m,

'#' )

> end
>end
>Regards
> robert

Cool !
Now. What about multi-lines asserts ?

Can't be done with the simple code above.

If that helps I may concede a end-of-xxx marker.

Well, the proper way would be to write a parser for a macro language. You
can get quite far with a simple hack like the one attached, although that
does only support nested macro invocations without using () for sub
macros. (Reason is that you can't parse nested brackets with regular
expressions.)

Have fun

    robert

mac-test.rb (56 Bytes)

test.rbm (98 Bytes)

Macro.rb (636 Bytes)

I wonder how adaptable camlp4 is.

martin

···

Robert Klemme <bob.news@gmx.net> wrote:

Well, the proper way would be to write a parser for a macro language. You
can get quite far with a simple hack like the one attached, although that
does only support nested macro invocations without using () for sub
macros. (Reason is that you can't parse nested brackets with regular
expressions.)

Perfect fro me. Thanks a lot. Ruby is amazing.

Yours,

JeanHuguesRobert

···

At 17:18 07/06/2004 +0900, you wrote:

"Jean-Hugues ROBERT" <jean_hugues_robert@yahoo.com> schrieb im Newsbeitrag
news:6.0.1.1.0.20040605205551.01e159b0@pop.mail.yahoo.com...

>Seriously, who needs CPP when there is Ruby?
>
>def load_special(file)
> if DEBUG
> load file
> else
> Object.new.instance_eval File.read(file).gsub(/^\s*assert\b/m,

'#' )

> end
>end
>Regards
> robert

Cool !
Now. What about multi-lines asserts ?

Can't be done with the simple code above.

If that helps I may concede a end-of-xxx marker.

Well, the proper way would be to write a parser for a macro language. You
can get quite far with a simple hack like the one attached, although that
does only support nested macro invocations without using () for sub
macros. (Reason is that you can't parse nested brackets with regular
expressions.)

Have fun

   robert

-------------------------------------------------------------------------
Web: http://hdl.handle.net/1030.37/1.1
Phone: +33 (0) 4 92 27 74 17