Professional Web Applications Themes

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

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 ...

  1. #1

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

    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 Guest

  2. #2

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

    Sean Ross wrote:
     

    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
    byu.edu

    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 Guest

  3. #3

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

    "Jamis Buck" <byu.edu> wrote in message
    news:byu.edu... 

    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 Guest

  4. #4

    Default 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

     

    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--

    Aredridel Guest

Similar Threads

  1. Replies: 1
    Last Post: April 24th, 01:27 PM
  2. CFINPUT type="radio" w/ "value" requires "label"
    By Iceborer in forum Macromedia ColdFusion
    Replies: 2
    Last Post: February 21st, 06:16 PM
  3. Replies: 0
    Last Post: November 7th, 11:45 AM
  4. dr("field").toString returns "400.0000" instead of "400"
    By Dan C Douglas in forum ASP.NET General
    Replies: 5
    Last Post: July 22nd, 05:48 PM
  5. "Start" "Program" "Menu" list is empty
    By Pete in forum Windows XP/2000/ME
    Replies: 2
    Last Post: July 10th, 10:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139