> 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:
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.
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:
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 }
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___)
"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
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:
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
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
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
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
"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.)
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.)
"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.)