Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning true or error message in Ruby

I'm wondering if writing functions like this is considered good or bad form.

def test(x)
    if x == 1
        return true
    else
        return "Error: x is not equal to one."
    end
end

And then to use it we do something like this:

result = test(1)

if result != true
    puts result
end

result = test(2)

if result != true
    puts result
end

Which just displays the error message for the second call to test.

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

def create
    @item = Item.new(params[:item])

    if [email protected]?
        result = @item.save_image(params[:attachment][:file])

        if result != true
            flash[:notice] = result

            redirect_to(new_item_url) and return
        end

        #and so on...

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.

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.

like image 959
seaneshbaugh Avatar asked May 08 '10 09:05

seaneshbaugh


People also ask

How do I return true or false in Ruby?

Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is all? will return true only if none of the collection members are false or nil.)

How do you catch errors in Ruby?

In Ruby, catch and throw blocks are the lightweight mechanism for error handling and used to jump from the exception when there is no additional work is available in the program. The catch block is used to jump out from the nested block and the block is labeled with a name.

How do you return in Ruby?

Ruby methods ALWAYS return the evaluated result of the last line of the expression unless an explicit return comes before it. If you wanted to explicitly return a value you can use the return keyword.

What is :& in Ruby?

In a method argument list, the & operator takes its operand, converts it to a Proc object if it isn't already (by calling to_proc on it) and passes it to the method as if a block had been used.


1 Answers

I would say that methods that return different types (e.g. boolean vs. string vs. numbers) under different circumstances are a bad practice.

If you have some sort of test method that wants to return details of why the test has not passed then you can return a pair of values (an Array) as follows:

def test(x)
    if x == 1
        return true, "x is fine"
    else
        return false, "Error: x is not equal to one."
    end
end

and then write the section of your controller code as:

valid, message = @item.save_image(params[:attachment][:file])

if !valid
  flash[:notice] = message
  redirect_to(new_item_url) and return
end

If you're talking about a save_image method that will succeed the majority of the time but may fail and you want to indicate this failure and the reason then I would use exceptions e.g.

def save_image(file)
  raise "No file was specified for saving" if file.nil? 
  # carry on trying to save image
end

and then your controller code would be along the lines of:

begin
  result = @item.save_image(params[:attachment][:file])
rescue Exception => ex
  flash[:notice] = ex.message
  redirect_to(new_item_url) and return
end
like image 64
mikej Avatar answered Oct 14 '22 13:10

mikej