Struct constructor

W

Wayne Magor

It isn't possible to define a class method [] (to make a constructor)
for a struct is it?

If you have:

Customer = Struct.new( "Customer", :name, :address, :zip )
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )

but you want it to be:

joe = ["Joe Smith", "123 Maple, Anytown NC", 12345]

Ruby will just make joe an array, right? No way around this that I
can see, is there?
 
S

Stefano Crocco

Alle gioved=EC 15 novembre 2007, Wayne Magor ha scritto:
It isn't possible to define a class method [] (to make a constructor)
for a struct is it?

If you have:

Customer =3D Struct.new( "Customer", :name, :address, :zip )
joe =3D Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )

but you want it to be:

joe =3D ["Joe Smith", "123 Maple, Anytown NC", 12345]

Ruby will just make joe an array, right? No way around this that I
can see, is there?

You can't instruct ruby to create a class different from Array for the [x,=
=20
y, ...] construct, but you can define a [] method for a class, and use it t=
o=20
create a new istance of the class:

class A
=20
def initialize x, y
@x =3D x
@y =3D y
end

def A.[](x, y)
new x, y
end

end

a =3D A[1, 2]

Since your Customer class was created using Struct.new, it already provides=
=20
this feature (at least, my trials show this, although I couldn't find=20
documentation about this). So, you can write:

joe =3D Customer["Joe Smith", "123 Maple, Anytown NC", 12345 ]

I hope this helps

Stefano
 
R

Robert Klemme

2007/11/15 said:
It isn't possible to define a class method [] (to make a constructor)
for a struct is it?

If you have:

Customer = Struct.new( "Customer", :name, :address, :zip )
joe = Customer.new( "Joe Smith", "123 Maple, Anytown NC", 12345 )

but you want it to be:

joe = ["Joe Smith", "123 Maple, Anytown NC", 12345]

Ruby will just make joe an array, right?

Of course you cannot change the semantics of the array constructor.
No way around this that I
can see, is there?

Of course there is, this is Ruby. :)

irb(main):001:0> Test = Struct.new :foo, :bar do
irb(main):002:1* class <<self
irb(main):003:2> alias :initialize :[]
irb(main):004:2> end
irb(main):005:1> end
=> Test
irb(main):006:0> Test[1,2]
=> #<struct Test foo=1, bar=2>
irb(main):007:0>

Kind regards

robert
 
W

Wayne Magor

Stefano said:
this feature (at least, my trials show this, although I couldn't find
documentation about this). So, you can write:

joe = Customer["Joe Smith", "123 Maple, Anytown NC", 12345 ]


I also missed the docs for this since I was looking in the old pick-axe
book included with Ruby dists. It's actually documented in the second
edition book on page 627. Thanks.

So how is [] defined in Ruby? Just curious. It must be a special-case.
 
M

MenTaLguY

So how is [] defined in Ruby? Just curious. It must be a special-case.

It's just a singleton method on the class object; "[]" is a valid method name.

joe = Customer.send:)[], "Joe Smith", "123 Maple, Anytown NC", 12345)

You can define a #[] method on anything.

-mental
 
S

Sebastian Hungerecker

Wayne said:
So how is [] defined in Ruby? Just curious.

obj[stuff] is syntactig sugar for obj.[](stuff) and obj[stuff]=otherstuff is
syntactic sugar for obj.[]=(stuff,otherstuff). Stuff can be multiple values
seperated by commas, otherstuff can't.

It must be a special-case.

Not more so than <, >, <=, >=, <<, >>, ~, ^, &, +, +@, -, -@, *, ** and so on.
Also all methods ending with a equals sign (where you can write obj.bla = blo
instead of obj.bla=(blo)).


HTH,
Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top