# Assertion reflection

**URL:** <https://www.ruby-forum.com/t/assertion-reflection/73412>\
**Category:** Ruby\
**Created:** [October 24, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/assertion-reflection/73412 "2006-10-24T16:07:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [October 24, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/assertion-reflection/73412/1 "2006-10-24T16:07:02Z")

</div>

[X-posted from the RSpec mailing list]

We discuss RSpec’s assertions, which use ‘should’ for ‘assert’.

Micah M. wrote:

> x.should\_equal 2  
> a.should\_not\_be nil

> When they fail they yield messages like:

> 1 should equal 2  
> nil should not be nil

> In my humble opinion, these messages should include their context and read  
> like this:
> 
> x should equal 2 but was 1  
> a should not be nil but was nil

This is why C++ is superior to Ruby. An assertion can reflect its  
expression  
into the diagnostic report like that.

Oh, and I mis-Googled this subject recently, and inadvertently learned  
that  
Python assertions now supports that reflection.

If I tell the maintainers that C++ can do something, they’l ignore me.  
But  
if I say Python can, they will doubtless see red and go crazy about it.

Here’s a snip of research that might still work:

def trace\_(&b)  
file, line\_nr, funkshun = caller()[0].split(’:’)

# sorry, folks - split’s the only stringey function I know 😉

```
    funkshun = funkshun.split(/in `/)[1]
    funkshun = funkshun.split(/\'/)[0]
    line_nr=line_nr.to_i
    linecnt=1
    expr=nil

    File.open(file).each_line do |line|
            if linecnt==line_nr
                    if line =~ /(^|\W)trace_\s*\{\s*(.*?)\s*\}/
                            expr = $2
                    end
                    break
            end
            linecnt+=1
    end

    if expr
            print file

```

puts “(#{line\_nr})#{funkshun} #{expr}: #{eval(expr, b).inspect}”  
end #{file}  
end

The intent was to let trace\_{foo} reflect both foo and its value.

RSpec could get by with fewer fragile regular expressions, and could  
simply  
reflect its entire line, then tack the variables on the end:

x.should\_equal 2 (1, 2)
