class

Struct

v2_6_3 - Show latest stable - Superclass: Object

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}!"
  end
end

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).

Included modules

  • Enumerable

Constants

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

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

Tms = An obsolete name of Process:

Files

  • ext/etc/etc.c
  • ext/json/lib/json/add/struct.rb
  • lib/pp.rb
  • process.c
  • struct.c

2Notes

Example

marcelo_murad · Jun 30, 20094 thanks

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

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

Passing a block with methods

tinogomes · Jan 28, 20111 thank

==== Code example

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

def longitude
  -2
end

def with_address
  "with #{address}"
end
end

g = Google.new("Some Addres")

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

==== Result

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