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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:08:38+00:00 2026-06-11T05:08:38+00:00

Is there any data structure in Ruby which is similar to Pascal records? I

  • 0

Is there any data structure in Ruby which is similar to Pascal records?

I would like to define a record type which has about 15-20 fields, all of them are strings.

I tried to define a class for this purpose, but I realized that I have to define getter and setter methods:

class Data
   def description
        @description
   end

   def size
        @size
   end

   def address
        @address
   end

   .
   .  all other (about 15 variables...)
   . 

   def description=( value )
        @description = value
   end

   def size=(value)
        @size=value
   end

   def address=(value)
        @address=value
   end
  .
  . more setter method (about 15 times...)
  .

end

To define all the 15-20 fields and getters/setters this way is quite annoying. Is there any shorter way to do it? For example:

class Data
 desc, size, address=""
end

Or something similar, and then I would be able to create a new instance of the Data class:

d=Data.new

and then set the instance variables:

d.desc="bla"
d.size="50.66"
d.address="Bla street 20."

I already have a method, that can parse an XML file with the XMLSimple gem, and I would like to create a record (or class) from the parsed data and return it back with the “return” keyword. Then I want to access the fields of the record simply: d.size, d.address, d.description and so on. For example:

def data_import(fname="1.xml")
  data = XmlSimple.xml_in(fname,{'SuppressEmpty' => ""})
  d=Data.new()
  d.desc=data['desc'][0]
  d.size=data['size'][0]
  d.address=data['address'][0]
  d. ... =
  d. ... =  (about 15-20 times)
  d. ... =
  return d
end

my XML (1.xml):

<entity>
 <desc>bla</desc>
 <size>50.66</size>
 <address>Bla street 20.</address>
 .
 . (15-20 more fields)
 .
</entity>

In the Pascal language (and as far as I know in C++) there were a data structure called “record” for this purpose.

  • 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-11T05:08:40+00:00Added an answer on June 11, 2026 at 5:08 am

    You cand use a Hash as a bucket for your attributes/properties or you can make use of Ruby’s metaprogramming abilities for generating getters and setters using accessors like this:

    attr_accessor :desc, :size, :address
    

    You can also collect the attributes in an array and use splatting like this to expand the array in a list needed by the previously mentioned method:

    attributes = [:desc, :size, :address]
    attr_accessor *attributes
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to create Set data structure(Unique Collections) like java in javascript?
Is there any data structure in C# that is like a dictionary but that
Is there any data structure in which locating a data is independent of its
HashMap<String, String> roleRightsID = new HashMap<String, String>(); is there any data structure similar to
Is there any tricky way to implement a set data structure (a collection of
Is there any way to get a map or other data structure of the
Is there any primitive data type that it's safe to not initialize? How about
Is there any way to change(increase) the data type of a column while saving
Are there any .NET frameworks for collecting data similar to Google Analytics, for example
Is there any maximum size for a cell of data in a DataTable (like

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.