0

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.

Benjamin
  • 1,832
  • 1
  • 17
  • 27
  • using backslash to escape the end of line character is now regarded as bad practice. Otherwise, your validates method looks just fine. Spend your time on more meaningfull things that this. – Marek Příhoda Dec 13 '11 at 21:54
  • Are you saying that if you split this line on comma without the brackets, it'll throw a syntax error? If so, can you post the error? – Srdjan Pejic Dec 13 '11 at 21:57
  • Additionally, this might belong better in http://codereview.stackexchange.com/ – Thong Kuah Dec 13 '11 at 22:05
  • There wasn't any one specific error, but I kept getting error on line blah because of blah. I meant to make the braces curly because I thought the whole thing was a hash at one point for some reason, apparently the version I posted is totally fine =) I was just trying to get at what's the proper way to split this across multiple lines without getting an error – Benjamin Dec 13 '11 at 22:15

1 Answers1

0

You have the right idea. I would write the validates macro thusly,

validates :email,
  :uniqueness => true,
  :length => {:within => 5..50},
  :format => {:with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i}

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

Thong Kuah
  • 3,263
  • 2
  • 19
  • 29
  • So are the arguments of validates a hash? As in would enclosing them in curly braces be the correct way if the braces were included? Or are they all arguments? – Benjamin Dec 13 '11 at 22:10
  • well, the arguments of validates is not a hash, it's a star or splat. If you look at http://api.rubyonrails.org/ for the official API, you will that validates has a method signature of `validates(*attributes)`. so if you look at the above, you are passing in two arguments, `:email` and the hash – Thong Kuah Dec 13 '11 at 22:27
  • Ahh I see. Thank you for the clarification and the quick response. – Benjamin Dec 13 '11 at 22:30