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

Discussion in 'Ruby' started by Gavin Kistner, Jan 29, 2004.

  1. 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 Kistner, Jan 29, 2004
    #1
    1. Advertising

  2. Gavin Kistner

    Chris Morris Guest

    Gavin Kistner wrote:

    > ....but you can't name an instance variable with one:
    >
    > attr_reader:)foo?)
    > def initialize
    > @foo?=true
    > end



    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 Morris, Jan 29, 2004
    #2
    1. Advertising

  3. --pf9I7BMVVzbSWLtt
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    > >....but you can't name an instance variable with one:
    > >
    > > attr_reader:)foo?)
    > > def initialize
    > > @foo?=3Dtrue
    > > end

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

    =20
    > Try changing it to attr_accessor and see if that works.


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

    iD8DBQFAGWZQCwWyMCTlrZkRAiKqAJ9bZvpBWSjyzopL3CXKAQ9xP5Bq2ACeJeky
    5RjrlLWjheEQi6Upsdw9MEI=
    =Sv7b
    -----END PGP SIGNATURE-----

    --pf9I7BMVVzbSWLtt--
    Zachary P. Landau, Jan 29, 2004
    #3
  4. Chris Morris wrote:
    >> attr_reader:)foo?)
    >> def initialize
    >> @foo?=true
    >> end

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


    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 Kistner, Jan 29, 2004
    #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:
    3
    Views:
    369
    John Roth
    Jul 29, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,682
    Smokey Grindel
    Dec 2, 2006
  3. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    375
    JaredGrubb
    Sep 24, 2008
  4. PerlFAQ Server
    Replies:
    0
    Views:
    238
    PerlFAQ Server
    Apr 26, 2011
  5. Replies:
    4
    Views:
    124
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page