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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:24:16+00:00 2026-05-27T20:24:16+00:00

They can be defined like this Struct.new(:x, :y) But what can usefully be done

  • 0

They can be defined like this

Struct.new(:x, :y)

But what can usefully be done with them? Specifically, how can I create an instance of such a struct? This doesn’t work

Struct.new(:x => 1, :y => 1)

(you get TypeError: can't convert Hash into String).

I’m using Ruby 1.9.2.

UPDATE:

Good pointers so far, thanks. I suppose the reason I asked this was that I have several times found myself wanting to do this

Struct.new(:x => 1, :y => 1)

just so that I can pass an object around where I can write obj.x instead of, say, instantiating a hash and having to write obj[:x]. In this case I want the structure to be really anonymous – I don’t want to pollute my namespace with anything by naming what is returned from the Struct.new call. The closest thing to that, as already suggested is

Struct.new(:x, :y).new(1, 1)

But how do you like them apples? I’m not sure I do. Is it reasonable to expect to be able to define and instantiate an anonymous struct in one go (as part of core Ruby)? I guess when I read the official Ruby docs on Struct.new I assume the word ‘anonymous’ allows this, but it doesn’t.

  • 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-27T20:24:17+00:00Added an answer on May 27, 2026 at 8:24 pm

    Struct.new returns a Class, so you can, for example, assign it to a constant like this:

    Point = Struct.new(:x, :y)
    

    or subclass it:

    class Point < Struct.new(:x, :y)
      # custom methods here
      # ...
    end
    

    In both cases, you can use the resulting class like this:

    Point.new(3, 5)
    

    If you don’t want to create a specific class (because you need to instantiate an object of that class only once), consider to use OpenStruct instead:

    require 'ostruct'
    
    point = OpenStruct.new(:x => 3, :y => 5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be prettty easy - but I just can't get
I have two containers, let's say they're defined like this: std::vector<std::unique_ptr<int>> a; std::vector<std::unique_ptr<int>> b;
Certain methods in Java will block until they can do something, like ServerSocket.accept() and
Pre-compiled headers seem like they can save a lot of time in large projects,
List comprehensions can be useful in certain situations, but they can also be rather
I'm sure this must have been asked before but I can't seem to find
I have my default route defined like this routes.MapRoute( Default, // Route name {controller}/{action}/{id},
this is probably something stupid, but i can't figure it out. I'm getting a
I have a class in which I have an enumeration, defined like this: class
If I have a javascript object/assoc. array defined like this: function somefunction(options) { var

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.