Check assertion in Ruby

E

Edgardo Hames

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

(e-mail address removed)
Tel: +54 +351 +5709800/01/02 int. 121
Av. Colón 778 - 4° piso. Córdoba
 
M

Michael Neumann

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
 
E

Edgardo Hames

Michael said:
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

(e-mail address removed)
Tel: +54 +351 +5709800/01/02 int. 121
Av. Colón 778 - 4° piso. Córdoba
 
G

Gregory Millam

Received: Sat, 5 Jun 2004 03:34:47 +0900
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
 
J

Jean-Hugues ROBERT

def check
raise "check failed" unless yield
end


check { result.num_rows == 1 }

Regards,

Michael


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
 
P

Paul Brannan

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.

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
 
D

David A. Black

Hi --

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
 
D

David A. Black

Hi --

Received: Sat, 5 Jun 2004 03:34:47 +0900


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
 
D

David Garamond

Paul said:
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 :)
 
R

Robert Klemme

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

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
 
R

Robert Klemme

David A. Black said:
Hi --



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
 
M

Michael Neumann

Hi --



I guess you could even do:

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

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
 
K

Kent Dahl

Michael said:
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,:eek:f,*arguments)
){|i| (i > 64 && i < 91) || (i > 96 && i < 123) }

IMHO
 
G

gabriele renzi

il Sat, 5 Jun 2004 18:37:43 +0900, Michael Neumann <[email protected]>
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 ?
 
D

David Garamond

Robert said:
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 :)
 
J

Jean-Hugues ROBERT

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

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

Yours,

Jean-Hugues
 
R

Robert Klemme

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
 
M

Martin DeMello

Robert Klemme said:
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.)

I wonder how adaptable camlp4 is.

martin
 
J

Jean-Hugues ROBERT

Can't be done with the simple code above.


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

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

Yours,

JeanHuguesRobert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top