Forum: Ruby on Rails DRY: need some help with blocks

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-03-27 23:58
Hi all

I have the following method in a model:

  def validate
    if !@errors.on(:abbrevation)
self.class.error_messages[:not_allowed_chars_in_abbrevation]) unless
abbrevation =~ /^[A-Z]+$/
    if !@errors.on(:abbrevation)
self.class.error_messages[:abbrevation_has_wrong_length]) unless
abbrevation.length == 2

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

while !@errors.on(:abbrevation) do
  # do some validation, and another validation, and another...

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? :-)

This topic is locked and can not be replied to.