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

Discussion in 'Ruby' started by Sean Ross, Dec 24, 2003.

  1. Sean Ross

    Sean Ross Guest

    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
     
    Sean Ross, Dec 24, 2003
    #1
    1. Advertising

  2. Sean Ross

    Jamis Buck Guest

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

    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


    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"'
     
    Jamis Buck, Dec 24, 2003
    #2
    1. Advertising

  3. Sean Ross

    Sean Ross Guest

    "Jamis Buck" <> wrote in message
    news:...
    > 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
     
    Sean Ross, Dec 24, 2003
    #3
  4. Sean Ross

    Aredridel Guest

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

    --=-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--
     
    Aredridel, Dec 25, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    686
  2. Julien Buratto

    [Job] Symbian C++ job

    Julien Buratto, Feb 3, 2004, in forum: C++
    Replies:
    2
    Views:
    576
    Julien Buratto
    Feb 3, 2004
  3. swordedotcom
    Replies:
    0
    Views:
    349
    swordedotcom
    Apr 23, 2006
  4. swordedotcom
    Replies:
    0
    Views:
    320
    swordedotcom
    Apr 23, 2006
  5. Kuang Dong
    Replies:
    3
    Views:
    97
    Gene Tani
    Sep 16, 2006
Loading...

Share This Page