# \[RCR\] attr\_reader :foo?

**URL:** <https://rubytalk.org/t/rcr-attr-reader-foo/6201>\
**Category:** ruby-talk\
**Created:** [31 May 2003 23:04 UTC](https://rubytalk.org/t/rcr-attr-reader-foo/6201 "2003-05-31T23:04:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [31 May 2003 23:04 UTC](https://rubytalk.org/t/rcr-attr-reader-foo/6201/1 "2003-05-31T23:04:16Z")

</div>

Would it make sense for attr\_reader and friends to associate :foo? with  
the attribute “@foo” ?

Consider the following code:

class C  
attr\_accessor :foo?  
def initialize  
@foo = true  
end  
end

c = C.new  
p c.foo? # ==\> false  
c.send(“foo?=”, true)  
p c.foo? # ==\> true  
p c.instance\_variables # ["@foo"]

You can call the reader. You have to bend over backwards to call the  
writer. You cannot access the attribute itself from within method  
definitions. You can’t even see that it exists using #instance\_variables.

Since the use of attr\_accessor construct is so pointless in this case,  
why not change the semantics:

“attr\_reader :foo?” defines a method #foo? which gets  
the value of @foo,

“attr\_writer :foo?” defines a method #foo= which sets  
the value of @foo.

This would not affect existing code because currently attr\_\* can’t  
effectively be used on a symbol ending in “?”.

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [31 May 2003 23:58 UTC](https://rubytalk.org/t/rcr-attr-reader-foo/6201/2 "2003-05-31T23:58:15Z")

</div>

Hi,

> **···**
>
> At Sun, 1 Jun 2003 08:04:16 +0900, Joel VanderWerf wrote:
> 
> > Since the use of attr\_accessor construct is so pointless in this case,  
> > why not change the semantics:
> > 
> > “attr\_reader :foo?” defines a method #foo? which gets  
> > the value of @foo,
> > 
> > “attr\_writer :foo?” defines a method #foo= which sets  
> > the value of @foo.
> 
> Like this?
> 
> # Index: eval.c
> 
> RCS file: /cvs/ruby/src/ruby/eval.c,v  
> retrieving revision 1.449  
> diff -u -2 -p -r1.449 eval.c  
> — eval.c 30 May 2003 16:07:25 -0000 1.449  
> +++ eval.c 31 May 2003 23:54:00 -0000  
> @@ -539,4 +539,5 @@ rb\_attr(klass, id, read, write, ex)  
> ID attriv;  
> int noex;
> 
> - int len;
> 
> - if (!name) {
> 
> - if (!name || !\*name) {  
> rb\_raise(rb\_eArgError, “argument needs to be symbol or string”);  
> }
> 
> - buf = ALLOCA\_N(char,strlen(name)+2);
> - sprintf(buf, “@%s”, name);
> 
> - len = strlen(name) - 1;
> - if (name[len] != ‘!’ && name[len] != ‘?’) len++;
> - buf = ALLOCA\_N(char, len+2);
> - buf[0] = ‘@’;
> - memcpy(buf+1, name, len);
> - buf[len+1] = ‘\0’;  
> attriv = rb\_intern(buf);  
> if (read) {  
> @@ -568,5 +573,6 @@ rb\_attr(klass, id, read, write, ex)  
> }  
> if (write) {
> 
> - sprintf(buf, “%s=”, name);
> 
> - memcpy(buf, name, len);
> - strcpy(&buf[len], “=”);  
> id = rb\_intern(buf);  
> rb\_add\_method(klass, id, NEW\_ATTRSET(attriv), noex);
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [1 June 2003 06:57 UTC](https://rubytalk.org/t/rcr-attr-reader-foo/6201/3 "2003-06-01T06:57:23Z")

</div>

instance\_variables works for me: I get  
[“@foo?”, “@foo”]

(version 1.6.8, FreeBSD)

But I agree such an instance variable is not directly accessible (e.g.  
@foo?.inspect fails with a parse error)

I guess the ? is being treated as part of the ? : operator rather than the  
variable name:

```
 @foo?.1:.2 # return .1 if @foo is true or .2 if @foo is false

```

Regards,

Brian.

> **···**
>
> On Sun, Jun 01, 2003 at 08:04:16AM +0900, Joel VanderWerf wrote:
> 
> > Consider the following code:
> > 
> > class C  
> > attr\_accessor :foo?  
> > def initialize  
> > @foo = true  
> > end  
> > end
> > 
> > c = C.new  
> > p c.foo? # ==\> false  
> > c.send(“foo?=”, true)  
> > p c.foo? # ==\> true  
> > p c.instance\_variables # [“@foo”]
> > 
> > You can call the reader. You have to bend over backwards to call the  
> > writer. You cannot access the attribute itself from within method  
> > definitions. You can’t even see that it exists using #instance\_variables.

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [1 June 2003 07:18 UTC](https://rubytalk.org/t/rcr-attr-reader-foo/6201/4 "2003-06-01T07:18:31Z")

</div>

Hi,

> instance\_variables works for me: I get  
> [“@foo?”, “@foo”]

attr\_(reader|writer|accessor) don’t create instance variable  
itself. When the attribute is assinged, the variable will be  
created.

> I guess the ? is being treated as part of the ? : operator rather than the  
> variable name:
> 
> ```
> @foo?.1:.2 # return .1 if @foo is true or .2 if @foo is false
> 
> ```

That’s right. ‘!’ and ‘?’ are allowed at the end of method  
names, but not as a part of variable names.

> **···**
>
> At Sun, 1 Jun 2003 15:57:23 +0900, Brian Candler wrote:
> 
> –  
> Nobu Nakada
