Is there a way to use "def self.new" to do the job of "def initialize"?

S

Sean Ross

Hi.
In Ruby, if I want to make an instance of a class C, the syntax is

c = C.new

But to define how that instance is initialized I need to make a method
called "initialize", not "new". Like so:

class C
attr_reader :a
def initialize
@a = "a"
end
end

I was curious, is there a way to do the initialization in a method "C.new"
without defining an "initialize" method?
Something like this:

class C
attr_reader :a
def C.new
# initialization
# return new instance of C
end
end

Why? Mostly curiousity. Ever since I've noticed the assymmetry between the
initialization syntax (c=C.new) and the initialization definition (def
initialize rather than def C.new) I've wondered if it would be possible to
use a more symmetrical method.

I realize that this is completely unneccessary and that people are happy
with the mechanisms that are in place, it's just that, for me, this a bit of
an itch to scratch.

Thank you for your time and attention,
Sean
 
J

Jamis Buck

Sean said:
class C
attr_reader :a
def C.new
# initialization
# return new instance of C
end
end

Well, keep in mind that in C.new, @a is not accessible. Thus, unless
'a' is a writable attribute, the C.new method cannot directly write to
it (thus the existence of the initialize method, to do instance-specific
initialization).

However, with eval magic, you can do it:

def C.new( a )
obj = allocate
obj.instance_eval { @a = a }
obj
end

It just feels kind of klunky to me (and an invasion of C's privacy) to
use instance_eval like that...

- Jamis
 
S

Sean Ross

Jamis Buck said:
However, with eval magic, you can do it:

def C.new( a )
obj = allocate
obj.instance_eval { @a = a }
obj
end

It just feels kind of klunky to me (and an invasion of C's privacy) to
use instance_eval like that...

Hi.

Thank you. I agree it does feel "klunky". But it can be done. Very cool.
I won't use it, I just wanted to know whether it could be done.

Thanks for satisfying my curiousity,
Sean
 
A

Aredridel

--=-PSxATH34AfP+WyA1naOX
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

Thank you. I agree it does feel "klunky". But it can be done. Very cool.
I won't use it, I just wanted to know whether it could be done.

It has its uses. Look in the net/http code and you'll find some clever
coding.

Ari

--=-PSxATH34AfP+WyA1naOX
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQA/6m68tP09exA3hooRAneZAJ4rLn9j499U79ykiCoTAvWY1LGycwCfRBcW
s2rYyzYTy24RSkqJ6ku9Yis=
=R+iT
-----END PGP SIGNATURE-----

--=-PSxATH34AfP+WyA1naOX--
 

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,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top