# Struct with defaults?

**URL:** <https://www.ruby-forum.com/t/struct-with-defaults/76212>\
**Category:** Ruby\
**Created:** [November 16, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/struct-with-defaults/76212 "2006-11-16T19:31:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [November 16, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/struct-with-defaults/76212/1 "2006-11-16T19:31:03Z")

</div>

is there an easy, elegant way to set up a Struct so it has defaults on  
initialization?

(besides def initialize?)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [November 17, 2006, 12:30am UTC](https://www.ruby-forum.com/t/struct-with-defaults/76212/2 "2006-11-17T00:30:29Z")

</div>

On Nov 16, 2006, at 12:30 PM, Giles B. wrote:

> is there an easy, elegant way to set up a Struct so it has defaults on  
> initialization?
> 
> (besides def initialize?)
> 
> –  
> Giles B.  
> [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

Not an exact answer to your question, but would using OpenStruct fit  
the bill?

#!/usr/bin/env ruby

require ‘ostruct’

user = OpenStruct.new({:name =\> ‘Bob’, :uid =\> 1234})  
p user.name  
p user.uid  
user.name = ‘Fred’  
p user.name

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [November 17, 2006, 10:31am UTC](https://www.ruby-forum.com/t/struct-with-defaults/76212/3 "2006-11-17T10:31:33Z")

</div>

On 17.11.2006 00:25, lists wrote:

> > 
> 
> p user.uid  
> user.name = ‘Fred’  
> p user.name

This just fills an instance with values. You would have to encapsulate  
that in a method like

def create() OpenStruct.new(:name =\> ‘Bob’, :uid =\> 1234) end

to match the OP’s requirements. That can also be done with a Struct

S = Struct.new(:name, :uid)  
def S.create() new(‘Bob’, 1234) end

irb(main):003:0\> S.create  
=\> #\<struct S name=“Bob”, uid=1234\>

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [November 17, 2006, 11:51am UTC](https://www.ruby-forum.com/t/struct-with-defaults/76212/4 "2006-11-17T11:51:38Z")

</div>

Hi,

At Fri, 17 Nov 2006 03:30:40 +0900,  
Giles B. wrote in [ruby-talk:225339]:

> is there an easy, elegant way to set up a Struct so it has defaults on  
> initialization?
> 
> (besides def initialize?)

class User \< Struct.new(:name, :uid)  
def initialize(name = ‘Bob’, uid = 1234)  
super  
end  
end

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [November 17, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/struct-with-defaults/76212/5 "2006-11-17T19:59:13Z")

</div>

that is easy and elegant! exactly as requested – thank you!
