# Assert

**URL:** <https://rubytalk.org/t/assert/14756>\
**Category:** ruby-talk\
**Created:** [12 November 2004 18:53 UTC](https://rubytalk.org/t/assert/14756 "2004-11-12T18:53:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)\
**Post date:** [12 November 2004 18:53 UTC](https://rubytalk.org/t/assert/14756/1 "2004-11-12T18:53:27Z")

</div>

I could not find a standard "assert" in Ruby. Is there one?

Thanks!

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [12 November 2004 19:54 UTC](https://rubytalk.org/t/assert/14756/2 "2004-11-12T19:54:21Z")

</div>

Outside of Test::Unit

raise "message" if condition

and

raise "meassge" unless condition

> **···**
>
> On Nov 12, 2004, at 10:53 AM, itsme213 wrote:
> 
> > I could not find a standard "assert" in Ruby. Is there one?

---

<div class="post-metadata">

**Author:** ![Ed\_Hames](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Ed\_Hames](https://rubytalk.org/u/Ed_Hames)\
**Post date:** [12 November 2004 20:09 UTC](https://rubytalk.org/t/assert/14756/3 "2004-11-12T20:09:50Z")

</div>

I asked a similar question some time ago. This is the thread:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/102412](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/102412)

Kind Regards,  
Ed

> **···**
>
> On Sat, 13 Nov 2004 03:53:27 +0900, itsme213 \<itsme213@hotmail.com\> wrote:
> 
> > I could not find a standard "assert" in Ruby. Is there one?
> 
> --  
> Despite the surge of power you feel upon learning Ruby,  
> resist the urge to trip others or slap them in the bald head.  
> DO NOT LORD YOUR RUBYNESS OVER OTHERS!

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [12 November 2004 20:28 UTC](https://rubytalk.org/t/assert/14756/4 "2004-11-12T20:28:26Z")

</div>

itsme213 wrote:

> I could not find a standard "assert" in Ruby. Is there one?

There's one in the dev-utils package. It will automatically set a breakpoint if the application is run in debug mode and therefore give you an irb shell where you can find out why your assert failed.

---

<div class="post-metadata">

**Author:** ![Mohammad\_Khan3](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@Mohammad\_Khan3](https://rubytalk.org/u/Mohammad_Khan3)\
**Post date:** [12 November 2004 20:24 UTC](https://rubytalk.org/t/assert/14756/5 "2004-11-12T20:24:15Z")

</div>

"Despite the surge of power you feel upon learning Ruby,  
resist the urge to trip others or slap them in the bald head.  
DO NOT LORD YOUR RUBYNESS OVER OTHERS!"

This is the most wonderful quote I ever heard about ruby.  
sorry to write off-topic .

> **···**
>
> On Fri, 2004-11-12 at 15:09, Edgardo Hames wrote:
> 
> > On Sat, 13 Nov 2004 03:53:27 +0900, itsme213 \<itsme213@hotmail.com\> wrote:  
> > \> I could not find a standard "assert" in Ruby. Is there one?  
> > \>
> > 
> > I asked a similar question some time ago. This is the thread:
> > 
> > [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/102412](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/102412)
> > 
> > Kind Regards,  
> > Ed
> 
> --  
> Mohammad

---

<div class="post-metadata">

**Author:** ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)\
**Post date:** [13 November 2004 00:38 UTC](https://rubytalk.org/t/assert/14756/6 "2004-11-13T00:38:25Z")

</div>

> There's one in the dev-utils package. It will automatically set a  
> breakpoint if the application is run in debug mode and therefore give  
> you an irb shell where you can find out why your assert failed.

That would work nice ... but I could not find it.

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [13 November 2004 01:46 UTC](https://rubytalk.org/t/assert/14756/7 "2004-11-13T01:46:23Z")

</div>

It's not there at the moment (I don't think). And the OP probably  
wants an exception raised, not an IRB session 🙂

Cheers,  
Gavin

> **···**
>
> On Saturday, November 13, 2004, 7:28:26 AM, Florian wrote:
> 
> > itsme213 wrote:
> 
> > > I could not find a standard "assert" in Ruby. Is there one?
> 
> > There's one in the dev-utils package. It will automatically set a  
> > breakpoint if the application is run in debug mode and therefore give  
> > you an irb shell where you can find out why your assert failed.

---

<div class="post-metadata">

**Author:** ![Ed\_Hames](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Ed\_Hames](https://rubytalk.org/u/Ed_Hames)\
**Post date:** [12 November 2004 21:50 UTC](https://rubytalk.org/t/assert/14756/8 "2004-11-12T21:50:29Z")

</div>

Qbviously, this quote belongs to why the lucky stiff. Today I feel  
like searching back in old mails...😉 Please, see this thread:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/117900](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/117900)

Regards,  
Ed

> **···**
>
> On Sat, 13 Nov 2004 05:24:15 +0900, Mohammad Khan \<mkhan@lextranet.com\> wrote:
> 
> > On Fri, 2004-11-12 at 15:09, Edgardo Hames wrote:
> > 
> > \> On Sat, 13 Nov 2004 03:53:27 +0900, itsme213 \<itsme213@hotmail.com\> wrote:  
> > \> \> I could not find a standard "assert" in Ruby. Is there one?  
> > \> \>  
> > \>  
> > \> I asked a similar question some time ago. This is the thread:  
> > \>  
> > \> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/102412](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/102412)  
> > \>  
> > \> Kind Regards,  
> > \> Ed
> > 
> > "Despite the surge of power you feel upon learning Ruby,  
> > resist the urge to trip others or slap them in the bald head.  
> > DO NOT LORD YOUR RUBYNESS OVER OTHERS!"
> > 
> > This is the most wonderful quote I ever heard about ruby.  
> > sorry to write off-topic .
> 
> --  
> Despite the surge of power you feel upon learning Ruby,  
> resist the urge to trip others or slap them in the bald head.  
> DO NOT LORD YOUR RUBYNESS OVER OTHERS!

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [13 November 2004 01:59 UTC](https://rubytalk.org/t/assert/14756/9 "2004-11-13T01:59:47Z")

</div>

> > itsme213 wrote:

> > > I could not find a standard "assert" in Ruby. Is there one?

> > There's one in the dev-utils package. It will automatically set a  
> > breakpoint if the application is run in debug mode and therefore give  
> > you an irb shell where you can find out why your assert failed.

> It's not there at the moment (I don't think). And the OP probably  
> wants an exception raised, not an IRB session 🙂

While writing that message, I realised why a breakpoint-inducing  
assert would be handy. Let's call it assert\_bp, since the bare name  
'assert' so strongly implies "raise an error if expression is false".

Code:

&nbsp;&nbsp;x = 5  
&nbsp;&nbsp;y = 6  
&nbsp;&nbsp;assert\_bp "x + y = 100"

Console:

&nbsp;&nbsp;Assert failed: x + y = 100 (foo/bar.rb:51)  
&nbsp;&nbsp;\> \_

Now, can anyone suggest a better name than 'assert\_bp'?

Gavin

> **···**
>
> On Saturday, November 13, 2004, 12:46:23 PM, Gavin wrote:
> 
> > On Saturday, November 13, 2004, 7:28:26 AM, Florian wrote:

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [13 November 2004 08:38 UTC](https://rubytalk.org/t/assert/14756/10 "2004-11-13T08:38:28Z")

</div>

itsme213 wrote:

> > There's one in the dev-utils package. It will automatically set a  
> > breakpoint if the application is run in debug mode and therefore give  
> > you an irb shell where you can find out why your assert failed.
> 
> That would work nice ... but I could not find it.

Oh, you are right. It is not yet included in dev-utils because of its name. I have attached my local version of breakpoint.rb which includes it.

I have thought about changing the name to "assume" so it can be included in dev-utils, but I'm not sure if that name is better.

[breakpoint\_client.rb](https://rubytalk.org/uploads/short-url/2j7iHpvASNV5I4U5MPkyklnIAo1.rb) (2.24 KB)

[binding\_of\_caller.rb](https://rubytalk.org/uploads/short-url/7Fbv7HZRtZSzUMawvJb1g4ImxNV.rb) (2.54 KB)

[breakpoint.rb](https://rubytalk.org/uploads/short-url/2lT4L5xrZryDyPDligXAz1rFtiP.rb) (9.57 KB)

---

<div class="post-metadata">

**Author:** ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)\
**Post date:** [13 November 2004 02:28 UTC](https://rubytalk.org/t/assert/14756/11 "2004-11-13T02:28:26Z")

</div>

> Now, can anyone suggest a better name than 'assert\_bp'?

assert?

i.e. tentative assert 🙂

though i would use it everywhere by default. it would be nice if there were  
a couple of eiffel-like switches to modify its behavior.

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [13 November 2004 13:28 UTC](https://rubytalk.org/t/assert/14756/12 "2004-11-13T13:28:25Z")

</div>

Maybe

&nbsp;&nbsp;breakpoint :unless =\> 'x + y == 10'

?

Gavin

> **···**
>
> On Saturday, November 13, 2004, 7:38:28 PM, Florian wrote:
> 
> > itsme213 wrote:
> 
> > > > There's one in the dev-utils package. It will automatically set a  
> > > > breakpoint if the application is run in debug mode and therefore give  
> > > > you an irb shell where you can find out why your assert failed.
> > > 
> > > That would work nice ... but I could not find it.
> 
> > Oh, you are right. It is not yet included in dev-utils because of its  
> > name. I have attached my local version of breakpoint.rb which includes it.
> 
> > I have thought about changing the name to "assume" so it can be included  
> > in dev-utils, but I'm not sure if that name is better.

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [17 November 2004 02:24 UTC](https://rubytalk.org/t/assert/14756/13 "2004-11-17T02:24:27Z")

</div>

#break\_unless  
#break\_if

> **···**
>
> On Nov 12, 2004, at 6:59 PM, Gavin Sinclair wrote:
> 
> > Now, can anyone suggest a better name than 'assert\_bp'?

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [13 November 2004 14:18 UTC](https://rubytalk.org/t/assert/14756/14 "2004-11-13T14:18:24Z")

</div>

Gavin Sinclair wrote:

> > I have thought about changing the name to "assume" so it can be included  
> > in dev-utils, but I'm not sure if that name is better.
> 
> Maybe
> 
> &nbsp;&nbsp;breakpoint :unless =\> 'x + y == 10'
> 
> ?

I think that would be way too much magic. I'd expect the method to just take a block.

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [13 November 2004 14:44 UTC](https://rubytalk.org/t/assert/14756/15 "2004-11-13T14:44:31Z")

</div>

Why a block? The breakpoint code uses Binding.of\_caller anyway, so a  
block is not needed for evaluation. I don't see any other reason,  
technical or stylistic, why a block should be preferred.

I suppose we might as well dispose of the symbolic argument and just  
do this:

&nbsp;&nbsp;breakpoint\_unless 'x + y == 10'

Can you expand on the "too much magic"?

Gavin

> **···**
>
> On Sunday, November 14, 2004, 1:18:24 AM, Florian wrote:
> 
> > Gavin Sinclair wrote:
> 
> > > > I have thought about changing the name to "assume" so it can be included  
> > > > in dev-utils, but I'm not sure if that name is better.
> > > 
> > > Maybe
> > > 
> > > &nbsp;&nbsp;breakpoint :unless =\> 'x + y == 10'
> > > 
> > > ?
> 
> > I think that would be way too much magic. I'd expect the method to just  
> > take a block.

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [13 November 2004 15:48 UTC](https://rubytalk.org/t/assert/14756/16 "2004-11-13T15:48:24Z")

</div>

Gavin Sinclair wrote:

> > > > I have thought about changing the name to "assume" so it can be included  
> > > > in dev-utils, but I'm not sure if that name is better.
> > 
> > I think that would be way too much magic. I'd expect the method to just  
> > take a block.
> 
> Why a block? The breakpoint code uses Binding.of\_caller anyway, so a  
> block is not needed for evaluation. I don't see any other reason,  
> technical or stylistic, why a block should be preferred.

Using strings for code is a bit ugly. We have blocks in Ruby, why not use them? Using a String also causes a bit of escaping trouble (I know this can be worked around via the %{} or \<\<-END syntax, but I still think blocks are clearer than that.)

> I suppose we might as well dispose of the symbolic argument and just  
> do this:
> 
> &nbsp;&nbsp;breakpoint\_unless 'x + y == 10'

Why don't just use "breakpoint unless x + y == 10" then? I think it makes sense to use something more concise here.

> Can you expand on the "too much magic"?

I don't know -- the syntax just seemed so very unnatural. Like something you wouldn't meet anywhere in the standard library...

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [13 November 2004 16:20 UTC](https://rubytalk.org/t/assert/14756/17 "2004-11-13T16:20:49Z")

</div>

> Gavin Sinclair wrote:

> > > > > I have thought about changing the name to "assume" so it can be included  
> > > > > in dev-utils, but I'm not sure if that name is better.
> > > 
> > > I think that would be way too much magic. I'd expect the method to just  
> > > take a block.
> > 
> > Why a block? The breakpoint code uses Binding.of\_caller anyway, so a  
> > block is not needed for evaluation. I don't see any other reason,  
> > technical or stylistic, why a block should be preferred.

> Using strings for code is a bit ugly. We have blocks in Ruby, why not  
> use them? Using a String also causes a bit of escaping trouble (I know  
> this can be worked around via the %{} or \<\<-END syntax, but I still  
> think blocks are clearer than that.)

Agreed, but you can print strings. You can't print blocks. See  
below.

> > I suppose we might as well dispose of the symbolic argument and just  
> > do this:
> > 
> > &nbsp;&nbsp;breakpoint\_unless 'x + y == 10'

> Why don't just use "breakpoint unless x + y == 10" then? I think it  
> makes sense to use something more concise here.

That option is still available. breakpoint\_unless 'x + y == 10' has  
the added benefit that the breakpoint can be identified by the  
condition that triggered it. E.g. you run the program and see on the  
console:

&nbsp;&nbsp;Breakpoint because 'x + y == 10' is false.  
&nbsp;&nbsp;\>

Is that a convincing benefit?

> > Can you expand on the "too much magic"?

> I don't know -- the syntax just seemed so very unnatural. Like something  
> you wouldn't meet anywhere in the standard library...

Debugging code often stands out in this unfortunate way, though. It's  
lower level than normal code.

Now let me ask you: since you proposed

&nbsp;&nbsp;assume { x + y == 10 }

what does that provide over

&nbsp;&nbsp;breakpoint unless x + y == 10

?

Gavin

> **···**
>
> On Sunday, November 14, 2004, 2:48:24 AM, Florian wrote:

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [13 November 2004 16:48 UTC](https://rubytalk.org/t/assert/14756/18 "2004-11-13T16:48:28Z")

</div>

Gavin Sinclair wrote:

> > Why don't just use "breakpoint unless x + y == 10" then? I think it makes sense to use something more concise here.
> 
> That option is still available. breakpoint\_unless 'x + y == 10' has  
> the added benefit that the breakpoint can be identified by the  
> condition that triggered it. E.g. you run the program and see on the  
> console:
> 
> &nbsp;&nbsp;Breakpoint because 'x + y == 10' is false.  
> &nbsp;&nbsp;\>
> 
> Is that a convincing benefit?

This does indeed sound nice.

> > I don't know -- the syntax just seemed so very unnatural. Like something  
> > you wouldn't meet anywhere in the standard library...
> 
> Debugging code often stands out in this unfortunate way, though. It's  
> lower level than normal code.

But note that this is something that can even remain in production code.

> Now let me ask you: since you proposed
> 
> &nbsp;&nbsp;assume { x + y == 10 }
> 
> what does that provide over
> 
> &nbsp;&nbsp;breakpoint unless x + y == 10
> 
> ?

I think it looks nicer and it can be ignored if the code is not run in debug mode.

What would you think about an assume() method that could take either a block or a String?

---

<div class="post-metadata">

**Author:** ![George5](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@George5](https://rubytalk.org/u/George5)\
**Post date:** [14 November 2004 00:13 UTC](https://rubytalk.org/t/assert/14756/19 "2004-11-14T00:13:13Z")

</div>

Hm, has someone tried ParseTree  
([zenspider projects | software projects | by ryan davis](http://www.zenspider.com/ZSS/Products/ParseTree/%5C)) to print out blocks?

Would be really great if that works.

&nbsp;&nbsp;trace { x + y } #=\> x+y = 5  
&nbsp;&nbsp;assert { x + y == 0 } #=\> assertion 'x+y==0' failed...

Uhm, seems that's only possible to dump methods in classes, not  
arbitrary code. But maybe, it's possible.

Regards,

&nbsp;&nbsp;Michael

> **···**
>
> On Sun, Nov 14, 2004 at 01:20:49AM +0900, Gavin Sinclair wrote:
> 
> > On Sunday, November 14, 2004, 2:48:24 AM, Florian wrote:
> > 
> > \> Gavin Sinclair wrote:
> > 
> > \>\>\>\>\>I have thought about changing the name to "assume" so it can be included  
> > \>\>\>\>\>in dev-utils, but I'm not sure if that name is better.  
> > \>\>\>I think that would be way too much magic. I'd expect the method to just  
> > \>\>\>take a block.  
> > \>\>  
> > \>\> Why a block? The breakpoint code uses Binding.of\_caller anyway, so a  
> > \>\> block is not needed for evaluation. I don't see any other reason,  
> > \>\> technical or stylistic, why a block should be preferred.
> > 
> > \> Using strings for code is a bit ugly. We have blocks in Ruby, why not  
> > \> use them? Using a String also causes a bit of escaping trouble (I know  
> > \> this can be worked around via the %{} or \<\<-END syntax, but I still  
> > \> think blocks are clearer than that.)
> > 
> > Agreed, but you can print strings. You can't print blocks. See  
> > below.

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [13 November 2004 17:09 UTC](https://rubytalk.org/t/assert/14756/20 "2004-11-13T17:09:04Z")

</div>

> > [... breakpoint\_unless 'x + y == 10' ...]
> > 
> > &nbsp;&nbsp;Breakpoint because 'x + y == 10' is false.  
> > &nbsp;&nbsp;\>
> > 
> > Is that a convincing benefit?

> This does indeed sound nice.

> > Now let me ask you: since you proposed
> > 
> > &nbsp;&nbsp;assume { x + y == 10 }
> > 
> > what does that provide over
> > 
> > &nbsp;&nbsp;breakpoint unless x + y == 10
> > 
> > ?

> I think it looks nicer and it can be ignored if the code is not run in  
> debug mode.

Are you suggesting that all breakpoint would only be enabled in debug  
mode, or just the 'assume' ones? What kind of debug mode do you mean?  
$DEBUG?

> What would you think about an assume() method that could take either a  
> block or a String?

I agree it looks nicer, but I feel that breakpoint\_unless is actually  
a more appropriate name, even though it's a little ugly. 'assume'  
doesn't tell you what it does. Setting breakpoints in this manner is  
such a specific and irregular thing to do that I think the name needs  
to be clear.

I'm happy with a method taking either a block or a string, though.

Gavin

> **···**
>
> On Sunday, November 14, 2004, 3:48:28 AM, Florian wrote:

[Next page](https://rubytalk.org/t/assert/14756.md?page=2)
