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

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

   def size

   def address

   .  all other (about 15 variables...)

   def description=( value )
        @description = value

   def size=(value)

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


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=""

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

and then set the instance variables:

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. ... =
  d. ... =  (about 15-20 times)
  d. ... =
  return d

my XML (1.xml):

 <address>Bla street 20.</address>
 . (15-20 more fields)

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


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

Yes, there is, it uses the C/C++ name instead of record: Struct. Struct is a class factory, if you call, it will return a class which has the desired fields:

Data =, :size, :address, …)

If you need to modify the behavior, you can pass a block to

Data =, :size, :address, …) do
  def my_custom_method; end

or use inheritance:

class Data <, :size, :address, …)
  def my_custom_method; end


d =

d.description = …
d.size = …
d.address = …

or alternatively:

d =…, …, …)

Need Your Help

Leading underscore for marking private members

javascript oop coding-style private jslint

As I know, in JavaScript there is no good solution to make private member. Solution described here is not efficient, because private members become parts of objects, not prototypes, and hence require

permission denied while trying to copy files from local server to remote server in ubuntu

linux ubuntu

I am working in a remote server,i need some files from my local server.I am using the following commands for copy files from local server to my remote server

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.