Tutorial by Examples

Struct defines new classes with the specified attributes and accessor methods. Person = Struct.new :first_name, :last_name You can then instantiate objects and use them: person = Person.new 'John', 'Doe' # => #<struct Person first_name="John", last_name="Doe"> p...
Person = Struct.new :name do def greet(someone) "Hello #{someone}! I am #{name}!" end end Person.new('Alice').greet 'Bob' # => "Hello Bob! I am Alice!"
Attributes can be accessed strings and symbols as keys. Numerical indexes also work. Person = Struct.new :name alice = Person.new 'Alice' alice['name'] # => "Alice" alice[:name] # => "Alice" alice[0] # => "Alice"

Page 1 of 1