Name

Struct — Structure class

Synopsis

Stuct is a abstract class that collects named attributes bundled in an object. You have to generate your own Struct class (subclass of Struct) using Struct::new, which returns new Struct class.

Example

S = Struct::new(:foo, :bar)
s = S::new(1,2)
s.foo              # => 1
s.bar = 5          # update the member
s.bar              # => 5
s                  # => #<S foo=1, bar=5>

Included Module

Enumerable

Class Method

Struct::new([name,] mem...)

Creates a new structure class containing members specified by mem... . If name is given, the structure class is bound to the constant under Struct, for example Struct::Passwd. Note that Struct::new doesn’t return a structure object itself, but rather a class that is used as a template for creating each structure.

Structure Class Methods

S::members

Returns an array of member names.

S::new(value...)

Creates a new structure object. value objects specify the initial value of each member and must match the number of members specified when the structure was created.

Instance Methods

s[mem]

Returns the value of member mem where mem is a symbol or integer. If mem is an integer, the value of the memth member is returned.

s[mem]=value

Sets the value of member mem. mem may be a symbol or integer.

s.each {|x|...}

Calls block once for each member.

s.members

Returns an array of member names.

s.values

Returns an array containing the value of each member.

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.