在Ruby中返回true或错误消息

时间:2021-12-09 07:34:35

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

我正在考虑这样做,因为在一个rails项目中,我正在我的控制器代码中工作,我调用模型的实例方法,如果出现问题,我希望模型将错误消息返回给控制器,控制器接受错误消息并将其放入闪存并重定向。有点像这样

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...

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.

这样我就不会在控制器中构造错误消息,只是传递它们,因为我真的不希望控制器关注save_image方法本身做什么,只是它是否有效。

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.

这对我来说很有意义,但我很好奇这是否被认为是写作方法的好或坏方式。请记住,我在最常见的意义上问这个主要涉及ruby,只是碰巧我在rails项目中这样做,控制器的实际逻辑真的不是我关心的问题。

1 个解决方案

#1


21  

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.

如果你在谈论一个save_image方法,它会在大部分时间内成功但可能会失败并且你想要指出这个失败和原因然后我会使用例外,例如

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

#1


21  

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.

如果你在谈论一个save_image方法,它会在大部分时间内成功但可能会失败并且你想要指出这个失败和原因然后我会使用例外,例如

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