Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Is there a way to use "def self.new" to do the job of "def initialize"?

Reply
Thread Tools

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

 
 
Sean Ross
Guest
Posts: n/a
 
      12-24-2003
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



 
Reply With Quote
 
 
 
 
Jamis Buck
Guest
Posts: n/a
 
      12-24-2003
Sean Ross wrote:

>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

--
Jamis Buck
http://www.velocityreviews.com/forums/(E-Mail Removed)

ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'



 
Reply With Quote
 
 
 
 
Sean Ross
Guest
Posts: n/a
 
      12-24-2003
"Jamis Buck" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Aredridel
Guest
Posts: n/a
 
      12-25-2003
--=-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/6m68tP09exA3hooRAneZAJ4rLn9j499U79ykiCoTAvWY1LGycw CfRBcW
s2rYyzYTy24RSkqJ6ku9Yis=
=R+iT
-----END PGP SIGNATURE-----

--=-PSxATH34AfP+WyA1naOX--

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any way to save current job to a file in irb? Kuang Dong Ruby 3 09-16-2006 04:50 PM
Find the perfect work at home job for you using our FREE Work at home Job Finder! maya Computer Information 1 06-11-2006 07:52 PM
Job title: $300 flat for job in perl, cgi, Visual Basic/Asp.Net, whatever online project swordedotcom Python 0 04-23-2006 05:35 AM
For Freshers & Job Seekers.. Sample Placement Papers, Job Details... zeronebdm@gmail.com Java 0 11-06-2005 08:03 AM
[Job] Symbian C++ job Julien Buratto C++ 2 02-03-2004 10:41 PM



Advertisments