title: Returning true or error message in Ruby
body: <p>I'm wondering if writing functions like this is considered good or bad form.</p>
  
  <pre><code>def test(x)
      if x == 1
          return true
      else
          return "Error: x is not equal to one."
      end
  end
  </code></pre>
  
  <p>And then to use it we do something like this:</p>
  
  <pre><code>result = test(1)
  
  if result != true
      puts result
  end
  
  result = test(2)
  
  if result != true
      puts result
  end
  </code></pre>
  
  <p>Which just displays the error message for the second call to test.</p>
  
  <p>I'm considering doing this because in a rails project I'm working on inside my controller code I make calls to a model's instance methods and if something goes wrong I want the model to return the error message to the controller and the controller takes that error message and puts it in the flash and redirects. Kinda like this</p>
  
  <pre><code>def create
      @item = Item.new(params[:item])
  
      if !@item.nil?
          result = @item.save_image(params[:attachment][:file])
  
          if result != true
              flash[:notice] = result
  
              redirect_to(new_item_url) and return
          end
  
          #and so on...
  </code></pre>
  
  <p>That way I'm not constructing the error messages in the controller, merely passing them along, because I really don't want the controller to be concerned with what the save_image method itself does just whether or not it worked.</p>
  
  <p>It makes sense to me, but I'm curious as to whether or not this is considered a good or bad way of writing methods. Keep in mind I'm asking this in the most general sense pertaining mostly to ruby, it just happens that I'm doing this in a rails project, the actual logic of the controller really isn't my concern.</p>