在 Ruby 中返回 true 或错误消息 [英] Returning true or error message in Ruby

查看:45
本文介绍了在 Ruby 中返回 true 或错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道编写这样的函数是好是坏.

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.

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

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

这样我就不会在控制器中构建错误消息,只是将它们传递出去,因为我真的不希望控制器关心 save_image 方法本身做了什么,只是它是否工作.

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.

这对我来说很有意义,但我很好奇这是否被认为是编写方法的好或坏方式.请记住,我是在最普遍的意义上问这个问题,主要与 ruby​​ 相关,碰巧我是在一个 Rails 项目中这样做,控制器的实际逻辑真的不是我关心的.

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.

推荐答案

我会说在不同情况下返回不同类型(例如布尔值、字符串和数字)的方法是一种不好的做法.

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

如果您有某种测试方法想要返回测试未通过的详细信息,那么您可以返回一对值(Array),如下所示:

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

如果您谈论的是 save_image 方法,该方法在大多数情况下会成功但可能会失败,并且您想指出此失败以及原因,那么我将使用 异常 例如

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

这篇关于在 Ruby 中返回 true 或错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆