Ruby on Rails 验证方法结构
我有一个相当简单的 ruby 语法问题(以及其他一些说明),但我一生都无法弄清楚。
上下文是我有非常常见的模型类子类化 ActiveRecord::Base,并且我正在利用验证。
我相信 Ruby 约定喜欢通过将长代码片段拆分为多行(如果这些行达到 80 行)来保持整洁,除非使用正则表达式很难做到这一点。我的第一个问题是:
如何正确拆分此验证行以使其正常工作?
validates :email, :uniqueness => true, :length => {:within => 5..50}, :format => {:with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i}
我尝试过类似的事情:
validates(
:email,
:uniqueness => true,
:length => {:within => 5..50},
:format => {:with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i}
)
我在 ruby convention 中读到过,你可以使用反斜杠来分割行,但我还没有尝试过,因为我认为这看起来有点奇怪,特别是当你可以通过确保使用 Ruby 的功能时逗号或操作数位于行尾。
我的最后一个问题是:
有人可以编写这个验证方法并使用所有适当的大括号和方括号吗?也许我对基本语法的去向有点困惑。
快速回顾:
如何正确拆分上面的单行验证?
你能用反斜杠分割 ruby 代码行吗?
有人写了用所有大括号和方括号编写的相同方法。
提前谢谢。
I have a fairly simple ruby syntax question (and a couple other clarifications), and I can't figure it out for the life of me.
The context is I have pretty common model class subclassing ActiveRecord::Base, and I am utilizing validates.
I believe Ruby convention likes keeping things neat by splitting long pieces of code across multiple lines if those lines go up to 80 lines unless it's something difficult to do that with like a regular expression. My first question is this:
How do I properly split up this validates line so it works properly?
validates :email, :uniqueness => true, :length => {:within => 5..50}, :format => {:with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i}
I have tried things like:
validates(
:email,
:uniqueness => true,
:length => {:within => 5..50},
:format => {:with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i}
)
I read in ruby convention somewhere that you could split lines using a backslash, and I haven't attempted it yet because I think that'd look a bit weird especially when you can utilize Ruby's power by just making sure a comma or operand is at the end of the line.
My final question is:
Could someone write this validates method with all the proper braces and brackets in place? Maybe I am a bit confused as to what basic syntax goes where.
Quick Recap:
How to split up single line validates above properly?
Can you split lines of ruby code with a backslash?
Someone write the same method written with all braces and brackets.
Thanks ahead of time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的想法是正确的。我会这样编写验证宏,
我们实际上不需要类宏的括号。第一行清楚地表明我们正在验证
:email
属性,后续行是对其进行各种验证。是的,您可以使用反斜杠,但通常不需要它,而且在我看来,它看起来很难看。最好以运算符结束,然后继续缩进的下一行。请参阅http://ruby-doc.org/docs/ProgrammingRuby/html/language。 如果可能的话,
我倾向于将 RegExp 文字全部保留在一行中。如果它太长,您可以开始使用
Regexp.new
You have the right idea. I would write the validates macro thusly,
We don't really need the enclosing brackets for a class macro. The first line would clearly indicate that we are validating the
:email
attribute, the subsequent lines are various validations on it.Yes, you can use backslash, but it's generally un-needed and imo, looks ugly to me. Better to end on an operator and then continue the next line indented. see http://ruby-doc.org/docs/ProgrammingRuby/html/language.html for an example viz-a-viz
I would tend to keep RegExp literals all in one line if possible. If it get's too long, you can start using
Regexp.new
instead