Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Why is def foo? legal but @foo? illegal?

Reply
Thread Tools

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

 
 
Gavin Kistner
Guest
Posts: n/a
 
      01-29-2004
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

--
(-, /\ \/ / /\/
 
Reply With Quote
 
 
 
 
Chris Morris
Guest
Posts: n/a
 
      01-29-2004
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



 
Reply With Quote
 
 
 
 
Zachary P. Landau
Guest
Posts: n/a
 
      01-29-2004
--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 <(E-Mail Removed)>
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--


 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      01-29-2004
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.

--
(-, /\ \/ / /\/
 
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
Newbie: def must come before call to def? planetthoughtful Ruby 4 03-12-2007 11:36 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
"def self.method" vs "class << self; def method" joevandyk@gmail.com Ruby 7 10-10-2006 08:46 AM
Is there a way to use "def self.new" to do the job of "def initialize"? Sean Ross Ruby 3 12-25-2003 04:59 AM
HttpModule -- how to intercept urls like http://localhost/abc/def or http://localhost/abc/def/ where abc, def are non virtual dir Jiong Feng ASP .Net 0 11-19-2003 05:29 AM



Advertisments