attr_reader :foo? 
Author Message
 attr_reader :foo?

Would it make sense for attr_reader and friends to associate :foo? with

Consider the following code:

   class C
     attr_accessor :foo?
     def initialize

     end
   end

   c = C.new
   p c.foo?               # ==> false
   c.send("foo?=", true)
   p c.foo?               # ==> true

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

   "attr_writer :foo?" defines a method #foo= which sets

This would not affect existing code because currently attr_* can't
effectively be used on a symbol ending in "?".



Thu, 17 Nov 2005 07:04:16 GMT  
 attr_reader :foo?
Hi,

At Sun, 1 Jun 2003 08:04:16 +0900,

Quote:

> 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

>    "attr_writer :foo?" defines a method #foo= which sets


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

     ID attriv;
     int noex;
+    int len;


     name = rb_id2name(id);
-    if (!name) {
+    if (!name || !*name) {
        rb_raise(rb_eArgError, "argument needs to be symbol or string");
     }
-    buf = ALLOCA_N(char,strlen(name)+2);

+    len = strlen(name) - 1;
+    if (name[len] != '!' && name[len] != '?') len++;
+    buf = ALLOCA_N(char, len+2);

+    memcpy(buf+1, name, len);
+    buf[len+1] = '\0';
     attriv = rb_intern(buf);

     }
     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



Thu, 17 Nov 2005 07:58:15 GMT  
 attr_reader :foo?

Quote:

> Consider the following code:

>   class C
>     attr_accessor :foo?
>     def initialize

>     end
>   end

>   c = C.new
>   p c.foo?               # ==> false
>   c.send("foo?=", true)
>   p c.foo?               # ==> true

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

instance_variables works for me: I get

(version 1.6.8, FreeBSD)


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


Regards,

Brian.



Thu, 17 Nov 2005 14:57:23 GMT  
 attr_reader :foo?
Hi,

At Sun, 1 Jun 2003 15:57:23 +0900,

Quote:

> instance_variables works for me: I get


attr_(reader|writer|accessor) don't create instance variable
itself.  When the attribute is assinged, the variable will be
created.

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



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

--
Nobu Nakada



Thu, 17 Nov 2005 15:18:31 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. A use for "foo = foo()"

2. if foo.__class__ == Foo: #in C?

3. foo.py vs. foo.pyc

4. Difference between $foo and [set foo]

5. eval $foo - does $foo get bytecompiled?

6. $foo == "" or [llength $foo] == 0

7. difference between ::foo::woof and foo::woof?

8. Date format from [file atime/mtime foo.foo]

9. attr_reader/writer vs. attr_accessor w/ access control

10. attr_reader, etc.

11. druby/attr_reader problem

12. attr_reader for great number of instance vars

 

 
Powered by phpBB® Forum Software