# Dynamic detection of bang and question methods

**URL:** <https://www.ruby-forum.com/t/dynamic-detection-of-bang-and-question-methods/114988>\
**Category:** Ruby\
**Created:** [September 19, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/dynamic-detection-of-bang-and-question-methods/114988 "2007-09-19T15:07:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leethal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@leethal](https://www.ruby-forum.com/u/leethal)\
**Post date:** [September 19, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/dynamic-detection-of-bang-and-question-methods/114988/1 "2007-09-19T15:07:45Z")

</div>

I got this idea of a sort of neat syntatic sugar of bang (foo!) and  
question (foo?) methods. Take a look at this pastie:  
[http://pastie.caboo.se/98638](http://pastie.caboo.se/98638)

The idea is that you’ll have two keywords added to Ruby - bang\_given?  
and question\_given?. Similar to block\_given?.

In this pastie, the method “finish” will not call save, as bang\_given?  
isn’t true. If you call “finish!”, though, b ang\_given will return true,  
and save will be called.

Same thing with question\_given?. Question given would be the least  
useful of them, though.

And yes, doing def finish!; finish; save; is pretty easy, and this is  
not the sort of thing that should have max priority. It would still be a  
nice addition to the code-design useability of Ruby, though, as having  
10 “def finish!; finish; save;”-ish methods is pretty boring.

(And yes, looping and define\_method, too, but still!)

---

<div class="post-metadata">

**Author:** ![leethal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@leethal](https://www.ruby-forum.com/u/leethal)\
**Post date:** [September 19, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/dynamic-detection-of-bang-and-question-methods/114988/2 "2007-09-19T16:49:39Z")

</div>

August L. wrote:

> I got this idea of a sort of neat syntatic sugar of bang (foo!) and  
> question (foo?) methods. Take a look at this pastie:  
> [Parked at Loopia](http://pastie.caboo.se/98638)

[http://pastie.caboo.se/98672](http://pastie.caboo.se/98672)  
You could implement something similar for ?-methods, but I don’t see the  
point. If you have a method foo! you almost always have method foo that  
does the same thing without changing the object - that’s hardly the case  
for foo? methods.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![leethal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@leethal](https://www.ruby-forum.com/u/leethal)\
**Post date:** [September 19, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/dynamic-detection-of-bang-and-question-methods/114988/3 "2007-09-19T16:59:40Z")

</div>

On 9/19/07, August L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Same thing with question\_given?. Question given would be the least  
> useful of them, though.
> 
> And yes, doing def finish!; finish; save; is pretty easy, and this is  
> not the sort of thing that should have max priority. It would still be a  
> nice addition to the code-design useability of Ruby, though, as having  
> 10 “def finish!; finish; save;”-ish methods is pretty boring.

It doesn’t seem to carry it’s own weight so as to merit the  
fundamental change to ruby semantics it implies. Ruby would need to  
magically turn a finish! call into a finish call and arrange to return  
true for bang\_given?

If it really bothers you to write those finish! methods for YOUR  
classes, why not do a little work with method\_missing or a class  
method to generate the pair which could be done without affecting the  
language or the base classes?

For example:

shadowfax:~/Documents rick$ cat magic\_bang.rb  
module MagicBang

def method\_missing(msg, \*args, &blk)  
msg.to\_s.sub(/(.+)!$/) do  
**send** ($1, \*args, &blk)  
return save  
end  
super  
end  
end

class TestClass  
include MagicBang

def finish  
puts “Finish called”  
end

def save  
puts “save called”  
end  
end

puts “Calling finish method”  
TestClass.new.finish  
puts  
puts “Calling finish! method”  
TestClass.new.finish!  
shadowfax:~/Documents rick$ ruby magic\_bang.rb  
Calling finish method  
Finish called

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![leethal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@leethal](https://www.ruby-forum.com/u/leethal)\
**Post date:** [September 20, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/dynamic-detection-of-bang-and-question-methods/114988/4 "2007-09-20T20:20:17Z")

</div>

August L. wrote:

> But then again, the effort it’ll take to implement this is probably not  
> worth it. Would be cool, but I guess it’s not useful enough.

Unless there’s something I’m missing, I _did_ implement this and  
actually it  
wasn’t too much effort at all.

---

<div class="post-metadata">

**Author:** ![leethal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e99b99/32.png) [@leethal](https://www.ruby-forum.com/u/leethal)\
**Post date:** [September 20, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/dynamic-detection-of-bang-and-question-methods/114988/5 "2007-09-20T20:07:00Z")

</div>

Rick Denatale wrote:

> shadowfax:~/Documents rick$ cat magic\_bang.rb  
> module MagicBang
> 
> def method\_missing(msg, \*args, &blk)  
> msg.to\_s.sub(/(.+)!$/) do  
> **send** ($1, \*args, &blk)  
> return save  
> end  
> super  
> end  
> end

I would, however, argue that bang\_given? would look a lot more clean.  
But then again, the effort it’ll take to implement this is probably not  
worth it. Would be cool, but I guess it’s not useful enough.
