A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.

The Struct class generates new subclasses that hold a set of members and their values. For each member a reader and writer method is created similar to Module#attr_accessor.

Customer = Struct.new(:name, :address) do
  def greeting
    "Hello #{name}!"

dave = Customer.new("Dave", "123 Main")
dave.name     #=> "Dave"
dave.greeting #=> "Hello Dave!"

See Struct::new for further examples of creating struct subclasses and instances.

In the method descriptions that follow a “member” parameter refers to a struct member which is either a quoted string (“name”) or a Symbol (:name).

Struct serialization/deserialization


Group = Define-const: Group Group is a Struct that is only available when compiled with +HAVE_GETGRENT+. The struct contains the following members: name:: contains the name of the group as a String. passwd:: contains the encrypted password as a String. An 'x' is returned if password access to the group is not available; an empty string is returned if no password is needed to obtain membership of the group. Must be compiled with +HAVE_STRUCT_GROUP_GR_PASSWD+. gid:: contains the group's numeric ID as an integer. mem:

Passwd = Define-const: Passwd Passwd is a Struct that contains the following members: name:: contains the short login name of the user as a String. passwd:: contains the encrypted password of the user as a String. an 'x' is returned if shadow passwords are in use. An '*' is returned if the user cannot log in using a password. uid:: contains the integer user ID (uid) of the user. gid:: contains the integer group ID (gid) of the user's primary group. dir:: contains the path to the home directory of the user as a String. shell:: contains the path to the login shell of the user as a String. === The following members below are optional, and must be compiled with special flags: gecos:: contains a longer String description of the user, such as a full name. Some Unix systems provide structured information in the gecos field, but this is system-dependent. must be compiled with +HAVE_STRUCT_PASSWD_PW_GECOS+ change:: password change time(integer) must be compiled with +HAVE_STRUCT_PASSWD_PW_CHANGE+ quota:: quota value(integer) must be compiled with +HAVE_STRUCT_PASSWD_PW_QUOTA+ age:: password age(integer) must be compiled with +HAVE_STRUCT_PASSWD_PW_AGE+ class:: user access class(string) must be compiled with +HAVE_STRUCT_PASSWD_PW_CLASS+ comment:: comment(string) must be compiled with +HAVE_STRUCT_PASSWD_PW_COMMENT+ expire:

Tms = rb_cProcessTms


Show files where this class is defined (5 files)
Register or log in to add new notes.
July 1, 2009
4 thanks


User = Struct.new(:name, :phone)

marc = User.new(“Marc”, “555-5555”)

February 12, 2012 - (v1_8_6_287 - v1_9_2_180)
2 thanks

More Examples


class User < Struct.new(:name, :age, :gender) 

user = User.new("Matz", 43, "male")
January 28, 2011 - (>= v1_8_7_72)
1 thank

Passing a block with methods

Code example

Google = Struct.new(:address) do
  def latitude

  def longitude

  def with_address
    "with #{address}"

g = Google.new("Some Addres")

puts g.address
puts g.latitude
puts g.longitude
puts g.with_address


# >> Some Addres
# >> -1
# >> -2
# >> with Some Addres