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 8886271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:31:03+00:00 2026-06-14T21:31:03+00:00

I wrote a very simple User class. The instance variable email has a reader

  • 0

I wrote a very simple User class. The instance variable email has a reader accessor and my own writer accessor that validates the email address with a regex.

class User
    attr_reader :email

    def email=(value)
        if (value =~ /^[a-z\d\-\_\+\.]+@([a-z\d\-]+\.)+[a-z]+$/)
            @email = value
        else
            # bonus question: is ArgumentError the right error type to use here?
            raise ArgumentError, "#{value} is not a valid email address."
        end
    end
end

I wrote the following test:

require 'test/unit'
require_relative '../lib/user'

class TC_UserTest < Test::Unit::TestCase
    def setup
        @user = User.new()
    end

    def test_email
        # using the writer accessor
        @user.email = 'user@example.com'
        # bypassing the writer accessor. evil.
        @user.email[4] = '#'
        assert_equal('user@example.com', @user.email)
    end
end

By using the reference given to me by the reader accessor, I am able to manipulate the email instance variable without going through the writer accessor.

The same principe would apply to any data type that allows manipulation without outright assigning a new value with =

Am I being overzealous? I just want to write robust code. Is there a way to ensure that my email address can only be set using the writer accessor?

I’m new to the language and I’m trying to get a feel for the best practices.

  • 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-06-14T21:31:04+00:00Added an answer on June 14, 2026 at 9:31 pm

    To do what you’re trying to do, my advice is to move the regexp into its own validation method.

    Better still, don’t write an email regexp unless you really want to do it right.

    Use a gem instead: https://github.com/SixArm/sixarm_ruby_email_address_validation

    After you set the email, freeze it with http://ruby-doc.org/core-1.9.3/Object.html#method-i-freeze

    Bonus answer: yes, ArgumentError is the right error type in general. If you’re using Rails, consider using the Rails validation methods.

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

Sidebar

Related Questions

I wrote a very simple web form that allows my user to view text
I wrote a very simple web form that allows my user to view text
I wrote a very simple mysql line for my very simple search bar that
I wrote this very simple macro to delete all rows when Column P has
I am Ubuntu 10.04 user and I wrote a very simple program, which I
I wrote a very simple .Net windows application that sits in the system tray
I wrote this very simple function for my current project called insidelabel() that let's
I just wrote a very simple perl tk script to read a huge text
I am trying to learn Android app development and wrote a very simple app
I am trying to write a very simple web-based email client from scratch with

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.