How to use a hash inside a Struct?

Is there a way to use a hash inside a Structure. Something like:

object = Struct.new(Hash.new(), :attr2, :attr3)

I know this doesn't work, but do you get the idea of what I am trying to do?

Jayson

Nope. What are you trying to do?

-Daniel

···

On 10/22/07, Jayson Williams <williams.jayson@gmail.com> wrote:

Is there a way to use a hash inside a Structure. Something like:

object = Struct.new(Hash.new(), :attr2, :attr3)

I know this doesn't work, but do you get the idea of what I am trying to
do?

Jayson

Struct objects except Symbols. What is it you're trying to do?

-- Thomas Adam

···

On 22/10/2007, Jayson Williams <williams.jayson@gmail.com> wrote:

Is there a way to use a hash inside a Structure. Something like:

object = Struct.new(Hash.new(), :attr2, :attr3)

I am creating a class that creates elements of a house. For the part
that creates new rooms, I wanted to link the room name with a room id.
There may be multiple rooms with the same name, but only one room with
the unique combination of name and id. I was thinking to use a Hash
inside the structure definition of the room for the name and id.

···

On 10/21/07, Daniel N <has.sox@gmail.com> wrote:

On 10/22/07, Jayson Williams <williams.jayson@gmail.com> wrote:
>
> Is there a way to use a hash inside a Structure. Something like:
>
> object = Struct.new(Hash.new(), :attr2, :attr3)
>
> I know this doesn't work, but do you get the idea of what I am trying to
> do?
>
> Jayson
>
>
Nope. What are you trying to do?

-Daniel

On 10/22/07, Thomas Adam <thomas.adam22@gmail.com> wrote:On 22/10/2007,

Is there a way to use a hash inside a Structure. Something like:

object = Struct.new(Hash.new(), :attr2, :attr3)

Do you mean to do something like this?:

Thing = Struct.new(:attr1,:attr2,:attr3)
t = Thing.new(Hash.new, nil, nil)

···

Jayson Williams <williams.jayson@gmail.com> wrote:

It might be easier to use OpenStruct.

require 'ostruct'

house = OpenStruct.new
house.room = {:room_id => 'id', :room_name => 'name'}
house.attr1 = 'some value'
etc...

···

On 10/22/07, Jayson Williams <williams.jayson@gmail.com> wrote:

I am creating a class that creates elements of a house. For the part
that creates new rooms, I wanted to link the room name with a room id.
There may be multiple rooms with the same name, but only one room with
the unique combination of name and id. I was thinking to use a Hash
inside the structure definition of the room for the name and id.

On 10/21/07, Daniel N <has.sox@gmail.com> wrote:
> On 10/22/07, Jayson Williams <williams.jayson@gmail.com> wrote:
> >
> > Is there a way to use a hash inside a Structure. Something like:
> >
> > object = Struct.new(Hash.new(), :attr2, :attr3)
> >
> > I know this doesn't work, but do you get the idea of what I am trying
to
> > do?
> >
> > Jayson
> >
> >
> Nope. What are you trying to do?
>
> -Daniel
>

--

"Every child has many wishes. Some include a wallet, two chicks and a cigar,
but that's another story."

I think both of these methods will work

Thing = Struct.new(:attr1,:attr2,:attr3)
t = Thing.new(Hash.new, nil, nil)

and...

require 'ostruct'

house = OpenStruct.new
house.room = {:room_id => 'id', :room_name => 'name'}
house.attr1 = 'some value'

I was thinking that the attributes in the structure had to be typed
first (a bit of c left in me). But it makes sense that I can set up
the attribute and put whatever I want there.
The second method looks a bit RubyTk'ish. I wonder if RubyTk uses OpenStruct.
Anyway, Thanks. This is just what I was looking for

Jayson

···

On 10/21/07, Christian <chippersbox@gmail.com> wrote:

It might be easier to use OpenStruct.

require 'ostruct'

house = OpenStruct.new
house.room = {:room_id => 'id', :room_name => 'name'}
house.attr1 = 'some value'
etc...

On 10/22/07, Jayson Williams <williams.jayson@gmail.com> wrote:
>
> I am creating a class that creates elements of a house. For the part
> that creates new rooms, I wanted to link the room name with a room id.
> There may be multiple rooms with the same name, but only one room with
> the unique combination of name and id. I was thinking to use a Hash
> inside the structure definition of the room for the name and id.
>
>
> On 10/21/07, Daniel N <has.sox@gmail.com> wrote:
> > On 10/22/07, Jayson Williams <williams.jayson@gmail.com> wrote:
> > >
> > > Is there a way to use a hash inside a Structure. Something like:
> > >
> > > object = Struct.new(Hash.new(), :attr2, :attr3)
> > >
> > > I know this doesn't work, but do you get the idea of what I am trying
> to
> > > do?
> > >
> > > Jayson
> > >
> > >
> > Nope. What are you trying to do?
> >
> > -Daniel
> >
>
>

--

"Every child has many wishes. Some include a wallet, two chicks and a cigar,
but that's another story."