Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6930997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:31:13+00:00 2026-05-27T11:31:13+00:00

I have a fairly simple ruby syntax question (and a couple other clarifications), and

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:31:13+00:00Added an answer on May 27, 2026 at 11:31 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple question: In Javascript how can I return the boolean
I have a fairly simple question about natural/surrogate key usage in a well-defined context
I have a fairly simple website where users can fill out a form describing
I have fairly simple problem but I can not think of the simple solution.
I have a fairly simple question concerning NSString however it doesn't seem to do
I have a fairly simple Hello World in X11 at end of question. But
I have a fairly simple ASP.NET 2.0 menu control using a sitemap file and
I have a fairly simple const struct in some C code that simply holds
I have a fairly simple addition to the HTTP standard. An ambitious goal I
I have a fairly simple sync problem. I have a table with about 10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.