# DRY: need some help with blocks

**URL:** <https://www.ruby-forum.com/t/dry-need-some-help-with-blocks/90339>\
**Category:** Rails\
**Created:** [March 27, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/dry-need-some-help-with-blocks/90339 "2007-03-27T23:58:32Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 27, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/dry-need-some-help-with-blocks/90339/1 "2007-03-27T23:58:32Z")

</div>

Hi all

I have the following method in a model:

def validate  
if !@errors.on(:abbrevation)  
@errors.add(:abbrevation,  
self.class.error\_messages[:not\_allowed\_chars\_in\_abbrevation]) unless  
abbrevation =~ /^[A-Z]+$/  
end  
if !@errors.on(:abbrevation)  
@errors.add(:abbrevation,  
self.class.error\_messages[:abbrevation\_has\_wrong\_length]) unless  
abbrevation.length == 2  
end  
end

I repeat if !@errors.on(:abbrevation) 2 times and maybe will have to  
repeat it again in future when adding more validations.

I’d really like to shorten this code, some sort of loop that runs as  
long as there is no error and in every loop another validation block is  
executed:

while !@errors.on(:abbrevation) do

# do some validation, and another validation, and another…

end

I guess I will have to use lambda or so, but I’m quite unsure how to use  
it appropriately in this case. Anyone has an idea? 🙂

Thanks  
Josh
