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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:42:59+00:00 2026-06-13T20:42:59+00:00

After creating a new Ruby OpenStruct object, I am able to store attributes but

  • 0

After creating a new Ruby OpenStruct object, I am able to store attributes but not to retrieve them (I get a blank line and it returns nil instead):

obj = OpenStruct.new # => #<OpenStruct>
obj.x = 10
obj.y = 20
obj                  # => #<OpenStruct x=10, y=20>
obj.x                # => 10
obj.y                #  
                     # => nil

If I try to store other properties with different names, everything works as expected. This problem seems to happen only when I store a property named y. I’m using the following version:

ruby 1.9.2p320 (2012-04-20 revision 35421) [i686-linux]

Does anybody have an idea of what’s going on?

  • 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-13T20:43:00+00:00Added an answer on June 13, 2026 at 8:43 pm

    Something somewhere is pulling in Psych for YAML stuff. Psych patches Kernel to add a psych_y method which is aliased to y. So, everything has a y method defined:

    > o = OpenStruct.new
    > o.method(:y)
     => #<Method: OpenStruct(Kernel)#psych_y> 
    

    AFAIK, OpenStruct uses method_missing and an internal Hash to produce accessor and mutator methods; but, there’s already a y from that “friendly” patch to Kernel so OpenStruct’s magic doesn’t get to handle the y method because Psych’s magic is in the way. The mutator, y=, is fine though so you can safely o.y = 11 and see your 11 inside o.

    You could remove the y like this:

    > o = OpenStruct.new
    > o.class_eval('undef_method :y')
    > o.y = 11
    > o.y
     => 11 
    

    You could probably remove the method from Kernel and hope that nothing depends on that silly y alias:

    > Kernel.send(:undef_method, :y)
    > o = OpenStruct.new
    > o.y = 11
    > o.y
     => 11 
    

    Or you could just remove it from OpenStruct:

    > OpenStruct.send(:undef_method, :y)
    > o = OpenStruct.new
    > o.y = 11
    > o.y
     => 11
    

    This sort of thing is why a lot of people don’t like monkey patching, especially monkey patching something as fundamental as Kernel.

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

Sidebar

Related Questions

After creating a new object (Foo), I set the key (BarId) for an EntityRef
I'm trying to handle event after creating new record to ExtJS store and saving
I've changed column length manually in my previous database. But after creating new database
I've installed virtualenv via pip and get this error after creating a new environment:
even after creating new object of unmarshaller every time, threads are getting blocked Please
After creating a new svn-depot and committing a file I get an empty log.
When I try to install gem like bundler or rake after creating new gemset
I have such view that handles user registration. After creating new user i want
After creating a new form, I usually perform this ritual: Change the name into
In many code samples, people usually use '\0' after creating a new char array

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.