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

  • Home
  • SEARCH
  • 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 7688117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:50:55+00:00 2026-05-31T19:50:55+00:00

Maybe I just don’t know enough about structs and have been using them blindly

  • 0

Maybe I just don’t know enough about structs and have been using them blindly but the result below seems irrational to me.

class VarTest < Struct.new(:email)
  def perform
    puts "Start: #{email}"
    if email == "nothing"
      email = "bad email"
    end
    puts "End: #{email}"
  end
end
VarTest.new("test@example.com").perform

Unexpected Output:

Start: test@example.com
End:

If I change the code to:

class VarTest < Struct.new(:email)
  def perform
    e = email
    puts "Start: #{e}"
    if e == "nothing"
      e = "bad email"
    end
    puts "End: #{e}"
  end
end

VarTest.new("test@example.com").perform

We get the expected output:

Start: test@example.com
End: test@example.com

Can someone please explain what is going on with this?

Thanks.

  • 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-31T19:50:57+00:00Added an answer on May 31, 2026 at 7:50 pm

    If you replace email = "bad email" with self.email = "bad email" it will work as expected. This is always true when using setters.

    The reason is simple: when Ruby encounters a bareword, it tries to resolve it as a local var. If there is none, it will try to call a method by that name. Inside the class body self is the implicit receiver, so readers just work. Now for the writer there’s a problem. If you write something like foo = "bar", Ruby will create a new local variable, hence you need to make the receiver explicit.

    This case is a bit trickier: if email == "nothing" uses the getter. However, email = "bad email" is still seen by the parser and a local variable email will be set to nil. This always happens when the parser sees a bareword as the LHS of an assignment. This local nil value is what makes it seem like the value of email disappears (which you can verify by only changing the last puts to puts "End: #{self.email}").

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

Sidebar

Related Questions

Maybe I just don't know .NET well enough yet, but I have yet to
Maybe I just don't know what to search for, but I'm going a little
Maybe I am over complicating this in my head but I just don't know
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
Maybe it's just doesn't exist, as I cannot find it. But using python's logging
I know for sure that this was already been asked before but I just
Maybe I'm slow, but I just don't get why you would ever use an
Not that it matters strictly, and maybe I just don't yet fully understand how
Maybe I just haven't figured out the InfoPath paradigm yet, but any links, or
I'm interested in texture streaming in DirectX but google seems unhelpful. Maybe I just

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.