Professional Web Applications Themes

Why is def foo? legal but foo? illegal? - Ruby

Can someone tell me why you can define a method with a question mark in its name... def initialize foo=true end def foo? foo end ....but you can't name an instance variable with one: attr_reader(:foo?) def initialize foo?=true end -- (-, /\ \/ / /\/...

  1. #1

    Default Why is def foo? legal but foo? illegal?

    Can someone tell me why you can define a method with a question mark in
    its name...

    def initialize
    foo=true
    end
    def foo?
    foo
    end


    ....but you can't name an instance variable with one:

    attr_reader(:foo?)
    def initialize
    foo?=true
    end

    --
    (-, /\ \/ / /\/
    Gavin Guest

  2. #2

    Default Re: Why is def foo? legal but foo? illegal?

    Gavin Kistner wrote:
     


    Don't know if it is possible, but I know the above won't work because
    you used attr_reader which will create a getter, but no setter for foo?.
    Try changing it to attr_accessor and see if that works.

    --
    Chris
    http://clabs.org



    Chris Guest

  3. #3

    Default Re: Why is def foo? legal but foo? illegal?

    --pf9I7BMVVzbSWLtt
    Content-Type: text/plain; cht=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable
     
    >=20
    > Don't know if it is possible, but I know the above won't work because=20
    > you used attr_reader which will create a getter, but no setter for foo?.=[/ref]
    =20 

    But he wasn't using the accessor in initialize, he was changing the
    variable directly. foo? =3D true would be using the accessor.

    --
    Zachary P. Landau <net>
    GPG: gpg --recv-key 0x24E5AD99 | http://kapheine.hypa.net/kapheine.asc

    --pf9I7BMVVzbSWLtt
    Content-Type: application/pgp-signature
    Content-Disposition: inline

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

    iD8DBQFAGWZQCwWyMCTlrZkRAiKqAJ9bZvpBWSjyzopL3CXKAQ 9xP5Bq2ACeJeky
    5RjrlLWjheEQi6Upsdw9MEI=
    =Sv7b
    -----END PGP SIGNATURE-----

    --pf9I7BMVVzbSWLtt--


    Zachary Guest

  4. #4

    Default Re: Why is def foo? legal but foo? illegal?

    Chris Morris wrote: 
    >
    > [...] but I know the above won't work because
    > you used attr_reader which will create a getter, but no setter for foo?.[/ref]

    What? You don't need a public setter function to set the value of an
    instance variable. That's the whole point of attr_reader :)

    For example, the following code works just fine:

    class Bar
    attr_reader(:foo)
    def initialize
    foo=true
    end
    end

    bar = Bar.new
    puts bar.foo


    as does this code:

    class Bar
    def initialize
    foo=true
    end
    def foo?
    return foo
    end
    end

    bar = Bar.new
    puts bar.foo?

    My only point is that if I could name an instance variable foo? then
    the code quoted at the top of this post would work, and be slightly less
    verbose.

    I suppose it's not allowed because you can define a method named foo?
    but not one named foo?= (thus enforcing the idea that a method ending in
    a question mark is for querying a boolean aspect of an instance, and not
    for setting one) and you might want to be able to publicly set the state
    of a foo? variable.

    --
    (-, /\ \/ / /\/
    Gavin Guest

Similar Threads

  1. OT: Need help if this is legal
    By dave milbut in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 21
    Last Post: August 14th, 01:11 PM
  2. Need help if this is legal
    By Cats 4ever in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 5
    Last Post: August 13th, 05:10 PM
  3. MS says pirating is illegal, but are their PA rules illegal?
    By PCyr in forum Windows Setup, Administration & Security
    Replies: 17
    Last Post: August 7th, 03:45 AM
  4. Replies: 19
    Last Post: August 7th, 03:45 AM

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