有没有比这个 Ruby 习惯更好的替代方案?

发布于 2024-08-17 17:58:38 字数 341 浏览 2 评论 0原文

我发现自己经常在控制器中编写这段代码:

params[:task][:completed_at] = Time.parse(params[:task][:completed_at]) if params[:task][:completed_at]

不要沉迷于我在这里具体做的事情,因为原因每次都会改变;但在很多情况下,我需要检查 params 中的值并在将其交给 createupdate_attributes 之前进行更改。

重复 params[:task][:completed_at] 三次感觉非常糟糕。有更好的方法吗?

I'm finding myself writing this bit of code in my controllers a lot:

params[:task][:completed_at] = Time.parse(params[:task][:completed_at]) if params[:task][:completed_at]

Don't get hung up on what I'm doing here specifically, because the reasons change every time; but there are many circumstances where I need to check for a value in params and change it before handing it off to create or update_attributes.

Repeating params[:task][:completed_at] three times feels very bad. Is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記憶穿過時間隧道 2024-08-24 17:58:38

稍微缩短这一过程的一种方法是:

if c = params[:task][:completed_at]
  params[:task][:completed_at] = Time.parse(c)
end

或者,您可能更喜欢这样:

params[:task][:completed_at] &&= Time.parse(params[:task][:completed_at])

在第二种情况下,仅当左侧为“真实”时才会发生分配。

One way to shorten this slightly is:

if c = params[:task][:completed_at]
  params[:task][:completed_at] = Time.parse(c)
end

Or, you might prefer this:

params[:task][:completed_at] &&= Time.parse(params[:task][:completed_at])

In the second case, the assignment will only happen if the left side is "truthy".

彼岸花似海 2024-08-24 17:58:38

我想你可以考虑做这样的事情。

在 String 和 NilClass 上实现 #to_time,可能在 extensions.rb 中(如 Ruby 最佳实践,例如,

require 'time'
class String
  def to_time
    Time.parse(self) # add error/exception handling to taste
  end
end

class NilClass
  def to_time
    nil
  end
end

然后您只需调用 params[:task][:created_at].to_time 即可,重复项就消失了。

我完全不确定这是否有效必然构成“最佳实践”,但恕我直言,它满足了问题的目标......

I suppose you could consider doing something like this.

Implement #to_time on String and NilClass, perhaps in a extensions.rb (as recommended in Ruby Best Practices, e.g.

require 'time'
class String
  def to_time
    Time.parse(self) # add error/exception handling to taste
  end
end

class NilClass
  def to_time
    nil
  end
end

Then you can just call params[:task][:created_at].to_time and the duplication is gone.

I'm not at all sure that this necessarily constitutes "best practice", but IMHO it meets the objective of the question...

执妄 2024-08-24 17:58:38

我对 Ruby 并不是非常熟悉,但由于它具有 Perl 根源,因此可能有一个构造允许您像这样编写它:

$_ = Time->parse($_) for params[:task][:completed_at] || ();

基本上利用 for 循环来创建变量的别名(如果存在),

可能类似于:

(params[:task][:completed_at] || ()).each { |i| i = Time.parse(i) }

编辑:

我发现 Ruby 有一个 alias 关键字。我对它不太熟悉,无法给出 Ruby 示例,但在 Perl 中,上面的内容也可以写成:

local *_ = \$params[$task][$completed_at];

$_ = Time->parse($_) if defined;

其中指定 $_ 将是 $params[$task] 的别名[$completed_at]

我尝试在 Ruby 中简单地使用它,但没有找到为标识符别名的方法,只有全局变量。

I am not incredibly familiar with Ruby, but since it has Perl roots, there may be a construct that allows you to write it like this:

$_ = Time->parse($_) for params[:task][:completed_at] || ();

basically exploiting the for loop to create an alias to the variable, if it exists

maybe something like:

(params[:task][:completed_at] || ()).each { |i| i = Time.parse(i) }

edit:

I see that Ruby has an alias keyword. I am not familiar enough with it to give a Ruby example, but in Perl, the above could also be written:

local *_ = \$params[$task][$completed_at];

$_ = Time->parse($_) if defined;

which specifies that $_ will be an alias for $params[$task][$completed_at]

I tried playing around with it breifly in Ruby, but didn't see a way to alias an identifier, just global variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文