# Before\_call, after\_call, assert, ... why not?

**URL:** <https://www.ruby-forum.com/t/before-call-after-call-assert-why-not/108369>\
**Category:** Ruby\
**Created:** [July 2, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/before-call-after-call-assert-why-not/108369 "2007-07-02T20:42:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dohzya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@dohzya](https://www.ruby-forum.com/u/dohzya)\
**Post date:** [July 2, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/before-call-after-call-assert-why-not/108369/1 "2007-07-02T20:42:59Z")

</div>

Hi, I am a new Ruby addict, and I’ve a question/suggestion (sorry…).  
I will try to explain myself by an example.  
I tried to add assertions possibility in ruby, but I think it’s not the  
best way to put them hardly in method body : I want to  
enable/disable/add/delete them easily. I thus added an assertion  
collection in Class, but how to force methods to call them ? Redefine  
**send**? Doesn’t work, and I don’t think it’s a good way. In my mind,  
a nice solution could be : every Proc can specify other Proc what will  
be called before/after them.

## some uses

class Truc  
def my\_method a, b  
# here: before\_call(name\_of\_proc, &the\_proc)  
before\_call {|a,b| raise “not same class” unless a.class ==  
b.class }  
a + b  
end  
end

# here: before\_call(method, name\_of\_proc=gensym(“proc\_”), &the\_proc)

Truc.before\_call :my\_method, :a\_name do |a,b|  
raise “a \> b” if a \> b  
end

# assert would be a shortcut for precious examples

## Truc.assert :my\_method, :name\_of\_assert, “message” do |a,b| #… end

## others uses

# name not needed ?

Machin.after\_call(:read\_a\_file){@file.close if @file.open?}

# Are Proc/before/after same binding ?

* * *

## some\_proc.before.each {|proc| …}

Is it envisageable ? (sorry if there is an discussion on this subject  
yet)
