# Foo= ... the only exception to the implicit-self rule?

**URL:** <https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363>\
**Category:** ruby-talk\
**Created:** [6 October 2005 04:16 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363 "2005-10-06T04:16:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)\
**Post date:** [6 October 2005 04:16 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/1 "2005-10-06T04:16:49Z")

</div>

Sorry to ask this again, but...

\*why\* does

&nbsp;&nbsp;foo  
seems to always means  
&nbsp;&nbsp;self.foo

except that  
&nbsp;&nbsp;foo= ...  
means  
&nbsp;&nbsp;local variable foo = ...  
and not  
&nbsp;&nbsp;self.foo=

I find this keeps breaking up an otherwise uniform style in code, and almost  
leads me towards abandoning #foo=(value) as a setter, and using #foo(value)  
instead (with some distinguished sentinel value to distinguish getter from  
setter, which I hate!)

I know that otherwise one would somehow have to indicate local variables.  
Was this the main reason for this choice?

Does this issue (distinguishing local variables) have to be dealt with  
anyway in any other contexts e.g. block-local variables?

Thanks.

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [6 October 2005 04:37 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/2 "2005-10-06T04:37:21Z")

</div>

Hi,

> I know that otherwise one would somehow have to indicate local variables.  
> Was this the main reason for this choice?

Yes.

> Does this issue (distinguishing local variables) have to be dealt with  
> anyway in any other contexts e.g. block-local variables?

Yes.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

> **···**
>
> In message "Re: foo= ... the only exception to the implicit-self rule ?" on Thu, 6 Oct 2005 13:16:49 +0900, "itsme213" \<itsme213@hotmail.com\> writes:

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [6 October 2005 04:56 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/3 "2005-10-06T04:56:48Z")

</div>

itsme213 wrote:

> Sorry to ask this again, but...
> 
> \*why\* does
> 
> &nbsp;&nbsp;foo  
> seems to always means  
> &nbsp;&nbsp;self.foo
> 
> except that  
> &nbsp;&nbsp;foo= ...  
> means  
> &nbsp;&nbsp;local variable foo = ...  
> and not  
> &nbsp;&nbsp;self.foo=
> 
> I find this keeps breaking up an otherwise uniform style in code, and almost  
> leads me towards abandoning #foo=(value) as a setter, and using #foo(value)  
> instead (with some distinguished sentinel value to distinguish getter from  
> setter, which I hate!)

I agree and ten not to use them for this reason. I suspect most others  
feel the same. One nice way to create a "setter" is:

&nbsp;&nbsp;def foo(val=NilClass)  
&nbsp;&nbsp;&nbsp;&nbsp;unless val == NilClass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@foo = val  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;@foo  
&nbsp;&nbsp;end

&nbsp;&nbsp;foo #=\> nil  
&nbsp;&nbsp;foo(10)  
&nbsp;&nbsp;foo #=\> 10

Facets has this by the way (in next release):

&nbsp;&nbsp;require 'facet/kernel/attr\_setter'

&nbsp;&nbsp;attr\_setter :foo

> I know that otherwise one would somehow have to indicate local variables.  
> Was this the main reason for this choice?

Yes. I think it was considered simply too "dangerous" to give setter  
methods precedence. I'm not sure why since there tend to be quite few  
of them, but I can see how it might catch one by suprise if their was a  
setter defined that one didn't know about.

Some have suggested allowing a prefixed dot to mean 'self.', which  
would help a little. Eg.

&nbsp;&nbsp;.foo=

But all other suggestions I can recall seem just as bad as having to  
use 'self.'

> Does this issue (distinguishing local variables) have to be dealt with  
> anyway in any other contexts e.g. block-local variables?

Do you mean defining vars local to a block? Perhaps?

&nbsp;&nbsp;&nbsp;{ x := 1 }

I can only imagine it has been suggested before though.

T.

---

<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:** [6 October 2005 05:31 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/4 "2005-10-06T05:31:34Z")

</div>

itsme213 wrote:  
...

> I find this keeps breaking up an otherwise uniform style in code, and almost  
> leads me towards abandoning #foo=(value) as a setter, and using #foo(value)  
> instead (with some distinguished sentinel value to distinguish getter from  
> setter, which I hate!)

You can avoid the sentinal (at a small cost):

def foo(\*args)  
&nbsp;&nbsp;case args.size  
&nbsp;&nbsp;when 1  
&nbsp;&nbsp;&nbsp;&nbsp;@foo = args[0]  
&nbsp;&nbsp;when 0  
&nbsp;&nbsp;&nbsp;&nbsp;@foo  
&nbsp;&nbsp;else raise ArgumentError, "Too many args, #{args.size}"  
&nbsp;&nbsp;end  
end

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)\
**Post date:** [6 October 2005 05:36 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/5 "2005-10-06T05:36:48Z")

</div>

Thanks Matz.

"Yukihiro Matsumoto" \<matz@ruby-lang.org\> wrote in message

> \>Does this issue (distinguishing local variables) have to be dealt with  
> \>anyway in any other contexts e.g. block-local variables?
> 
> Yes.

Have you decided whether 2.0 will require all local vars to be distinguished  
via some syntax (e.g. |...|), or will 2.0 continue the current special-case  
handling for self.foo= ?

---

<div class="post-metadata">

**Author:** ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)\
**Post date:** [6 October 2005 07:31 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/6 "2005-10-06T07:31:48Z")

</div>

True.

However, the bigger issue is that writer and reader are now embedded in a  
single method, and its impossible to do things like extend the writer to do  
notifications and the like.

I guess a set\_x(val) is the other alternative. Not sure which I dislike more  
☹

"Joel VanderWerf" \<vjoel@path.berkeley.edu\> wrote in message  
news:4344B6AC.7070400@path.berkeley.edu...

> itsme213 wrote:  
> ..  
> \> I find this keeps breaking up an otherwise uniform style in code, and

almost

> \> leads me towards abandoning #foo=(value) as a setter, and using

#foo(value)

> \> instead (with some distinguished sentinel value to distinguish getter

from

> **···**
>
> > \> setter, which I hate!)
> > 
> > You can avoid the sentinal (at a small cost):
> > 
> > def foo(\*args)  
> > &nbsp;&nbsp;case args.size  
> > &nbsp;&nbsp;when 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;@foo = args[0]  
> > &nbsp;&nbsp;when 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;@foo  
> > &nbsp;&nbsp;else raise ArgumentError, "Too many args, #{args.size}"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > --  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [6 October 2005 16:11 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/7 "2005-10-06T16:11:48Z")

</div>

Yukihiro Matsumoto wrote:

> Hi,
> 
> \>I know that otherwise one would somehow have to indicate local variables.  
> \>Was this the main reason for this choice?
> 
> Yes.

I'm not sure local precedence is worth it though. Consider:

&nbsp;&nbsp;require 'yaml'  
&nbsp;&nbsp;eqmeths =   
&nbsp;&nbsp;ObjectSpace.each\_object{|o|  
&nbsp;&nbsp;&nbsp;&nbsp;s = o.methods.select{ |m| m.to\_s =~ /=$/ }  
&nbsp;&nbsp;&nbsp;&nbsp;eqmeths |= s  
&nbsp;&nbsp;}  
&nbsp;&nbsp;y eqmeths

> **···**
>
> > In message "Re: foo= ... the only exception to the implicit-self rule ?" \> on Thu, 6 Oct 2005 13:16:49 +0900, "itsme213" \<itsme213@hotmail.com\> writes:
> 
> &nbsp;&nbsp;---  
> &nbsp;&nbsp;- "="  
> &nbsp;&nbsp;- "=="  
> &nbsp;&nbsp;- "==="  
> &nbsp;&nbsp;- "\>="  
> &nbsp;&nbsp;- "\<="  
> &nbsp;&nbsp;- ui=  
> &nbsp;&nbsp;- debug=  
> &nbsp;&nbsp;- sync=  
> &nbsp;&nbsp;- lineno=  
> &nbsp;&nbsp;- pos=  
> &nbsp;&nbsp;- default=  
> &nbsp;&nbsp;- ssl\_available=  
> &nbsp;&nbsp;- uid=  
> &nbsp;&nbsp;- euid=  
> &nbsp;&nbsp;- gid=  
> &nbsp;&nbsp;- egid=  
> &nbsp;&nbsp;- groups=  
> &nbsp;&nbsp;- maxgroups=  
> &nbsp;&nbsp;- abort\_on\_exception=  
> &nbsp;&nbsp;- priority=  
> &nbsp;&nbsp;- critical=  
> &nbsp;&nbsp;- families=  
> &nbsp;&nbsp;- private\_types=  
> &nbsp;&nbsp;- anchors=  
> &nbsp;&nbsp;- bufsize=  
> &nbsp;&nbsp;- version=  
> &nbsp;&nbsp;- verify\_data=  
> &nbsp;&nbsp;- only\_signed=  
> &nbsp;&nbsp;- verify\_signer=  
> &nbsp;&nbsp;- verify\_chain=  
> &nbsp;&nbsp;- verify\_root=  
> &nbsp;&nbsp;- only\_trusted=
> 
> T.

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [6 October 2005 09:30 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/8 "2005-10-06T09:30:33Z")

</div>

Hi,

> **···**
>
> In message "Re: foo= ... the only exception to the implicit-self rule ?" on Thu, 6 Oct 2005 14:36:48 +0900, "itsme213" \<itsme213@hotmail.com\> writes:
> 
> > Have you decided whether 2.0 will require all local vars to be distinguished  
> > via some syntax (e.g. |...|), or will 2.0 continue the current special-case  
> > handling for self.foo= ?
> 
> I have never planned for any kind of explicit local variable  
> declaration. So the latter would be an answer.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [6 October 2005 13:51 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/9 "2005-10-06T13:51:16Z")

</div>

I'm amazed that you find either the above or foo(x) more appealing than simply using self.foo = x. In my personal opinion, the assignment is clear, and made clearer by the presence of 'self'.

The only alternative that I would ever (personally) endorse would be some sort of sigil for local variables like there is for global, instance, and class variables. (Ick, more punctuation. Ick, more typying. Ick, breaks backwards compatibility. But: at least it would be consistent with other variables.) Something like:  
%foo = 12 #The randomly-chosen % means local variable.  
foo = 12 #Method call with implicit self.

But...really, I'm reasonably happy with the way things are now.

> **···**
>
> On Oct 6, 2005, at 1:31 AM, itsme213 wrote:
> 
> > I guess a set\_x(val) is the other alternative. Not sure which I dislike more  
> > ☹

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [6 October 2005 14:23 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/10 "2005-10-06T14:23:12Z")

</div>

This would really make ruby ugly. One of the beauties about  
ruby is that you don't have to declare your variables (type or  
existance). There are currently deficiencies in the  
flexibility of variable scoping (only def/class/module can make  
real local variables as far as I know), but I would hope that  
could be solved without adding variable declarations.

Take a look at my RCR 307. In this one, you can do this:

obj.foo(arg1,arg2) = value

which would be equivalent to:

obj.send(:foo=,arg1,arg2,value)

Well, there is no reason this RCR couldn't handle the no  
receiver case:

foo(arg1,arg2) = value

And the no receiver/no arg case:

foo() = value

which would be equivalent to:

self.foo = value

Just like the case when you already are using a local variable  
"foo" where you use "foo()" to call the method foo, the same  
can be done for assignment.

But, I think the self.foo=value syntax would still be preferred  
by most. I'm not sure why you dislike it so much to propose  
making such radical changes.

We are also talking about inside the class, so you know the  
implementation. Unless foo= is complex, you might want to just  
inline it whenever you want to use it (typically @foo=value).  
This will give you a performance boost since you are cutting  
out a method call (runtime is probably dominated by the method  
call mechanism in most cases).

> **···**
>
> --- itsme213 \<itsme213@hotmail.com\> wrote:
> 
> > Have you decided whether 2.0 will require all local vars to  
> > be distinguished  
> > via some syntax (e.g. |...|), or will 2.0 continue the  
> > current special-case  
> > handling for self.foo= ?
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [6 October 2005 17:17 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/11 "2005-10-06T17:17:29Z")

</div>

Hi --

> **···**
>
> On Fri, 7 Oct 2005, Trans wrote:
> 
> > Yukihiro Matsumoto wrote:
> > 
> > > Hi,
> > > 
> > > In message "Re: foo= ... the only exception to the implicit-self rule ?" \>\> on Thu, 6 Oct 2005 13:16:49 +0900, "itsme213" \<itsme213@hotmail.com\> writes:
> > > 
> > > \>I know that otherwise one would somehow have to indicate local variables.  
> > > \>Was this the main reason for this choice?
> > > 
> > > Yes.
> > 
> > I'm not sure local precedence is worth it though. Consider:
> > 
> > require 'yaml'  
> > eqmeths =   
> > ObjectSpace.each\_object{|o|  
> > &nbsp;&nbsp;&nbsp;s = o.methods.select{ |m| m.to\_s =~ /=$/ }  
> > &nbsp;&nbsp;&nbsp;eqmeths |= s  
> > }  
> > y eqmeths
> > 
> > ---  
> > - "="  
> > - "=="  
> > - "==="  
> > - "\>="  
> > - "\<="  
> > - ui=  
> > - debug=  
> > - sync=  
> > - lineno=  
> > - pos=  
> > - default=  
> > - ssl\_available=  
> > - uid=  
> > - euid=  
> > - gid=  
> > - egid=  
> > - groups=  
> > - maxgroups=  
> > - abort\_on\_exception=  
> > - priority=  
> > - critical=  
> > - families=  
> > - private\_types=  
> > - anchors=  
> > - bufsize=  
> > - version=  
> > - verify\_data=  
> > - only\_signed=  
> > - verify\_signer=  
> > - verify\_chain=  
> > - verify\_root=  
> > - only\_trusted=
> 
> Can you explain what you mean? I'm not getting it.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [6 October 2005 14:15 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/12 "2005-10-06T14:15:06Z")

</div>

> > I guess a set\_x(val) is the other alternative. Not sure which I dislike more  
> > ☹
> 
> I'm amazed that you find either the above or foo(x) more appealing than simply using self.foo = x. In my personal opinion, the assignment is clear, and made clearer by the presence of 'self'.

[snip]

> But...really, I'm reasonably happy with the way things are now.

I believe I have only ever run into this problem when using instance\_eval(). Is it more pervasive than that?

With instance\_eval() it could be argued that I'm already making questionable choices, and this is just punishment, I think. 😉

James Edward Gray II

> **···**
>
> On Oct 6, 2005, at 8:51 AM, Gavin Kistner wrote:
> 
> > On Oct 6, 2005, at 1:31 AM, itsme213 wrote:

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [6 October 2005 14:35 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/13 "2005-10-06T14:35:07Z")

</div>

> > I guess a set\_x(val) is the other alternative. Not sure which I dislike more  
> > ☹
> 
> I'm amazed that you find either the above or foo(x) more appealing than simply using self.foo = x. In my personal opinion, the assignment is clear, and made clearer by the presence of 'self'.

it's just my opinion, but i think this is clear as a whistle:

&nbsp;&nbsp;&nbsp;require 'traits'

&nbsp;&nbsp;&nbsp;class Widget  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;traits %w( colour width height style )  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;class Button \< Widget  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color 'blue'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width 42  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height 42  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sytle 'swirly'  
&nbsp;&nbsp;&nbsp;end

and this

&nbsp;&nbsp;&nbsp;require 'traits'

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;traits(TRAITS = %w( a b c d e f ))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def initialize \*args  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TRAITS.each{|t| send t, args.shift}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

visually the reader/writer method (writer if given arg) can be very, very nice  
on the eyes and fingers - i just takes getting used to, like dropping the ';'  
and '$' when moving from perl to ruby. 😉

i espcially like that i can do

&nbsp;&nbsp;&nbsp;def init hash = {}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash.each{|k,v| send k, v}  
&nbsp;&nbsp;&nbsp;end

instead of

&nbsp;&nbsp;&nbsp;def init hash = {}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash.each do |k,v|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;meth = "#{ k }="  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send meth, v  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

which always feels hackish to me

> The only alternative that I would ever (personally) endorse would be some  
> sort of sigil for local variables like there is for global, instance, and  
> class variables. (Ick, more punctuation. Ick, more typying. Ick, breaks  
> backwards compatibility. But: at least it would be consistent with other  
> variables.) Something like: %foo = 12 #The randomly-chosen % means local  
> variable. foo = 12 #Method call with implicit self.

i think a symbol for local vars makes a TON of sense and is, really, in  
keeping with ruby's design since every other scope is signaled by a symbol.

the rule could be

&nbsp;&nbsp;&nbsp;foo = 42

&nbsp;&nbsp;&nbsp;# if method exists foo=, use it  
&nbsp;&nbsp;&nbsp;# else local var

&nbsp;&nbsp;&nbsp;foo := 42 # force local var

or

&nbsp;&nbsp;&nbsp;!foo = 42 # force local var  
&nbsp;&nbsp;&nbsp;%foo = 42 # force local var

the thing with local vars is that, if ruby ALSO changed to lookup to search  
for methods FIRST, and then to fall back on local you really wouldn't have to  
use the local symbol/marker exectp in cases where a local var shadows a  
method in the scope of object instance.

> But...really, I'm reasonably happy with the way things are now.

me too.

cheers.

-a

> **···**
>
> On Thu, 6 Oct 2005, Gavin Kistner wrote:
> 
> > On Oct 6, 2005, at 1:31 AM, itsme213 wrote:
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > Your life dwells amoung the causes of death  
> > Like a lamp standing in a strong breeze. --Nagarjuna
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [6 October 2005 18:16 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/14 "2005-10-06T18:16:52Z")

</div>

> Can you explain what you mean? I'm not getting it.

Sure. Simply that there are very few setter methods built-in to Ruby.  
On top of which almost all are particular to very special contexts. So  
there's very little chance of surprise if setter methods were in fact  
to have precendence over locals. -- In other words, one would know  
every setter and thus know to avoid using it.

OTOH, I suppose it might present some issues with dynamic methods.

Here's another list by Class/Module instance\_method. Between this list  
and the prior I think that all of them --and there's quite a bit of  
overlap.

&nbsp;&nbsp;require 'yaml'

&nbsp;&nbsp;eqmeths = {}  
&nbsp;&nbsp;[Module,Class].each { |mod|  
&nbsp;&nbsp;&nbsp;&nbsp;ObjectSpace.each\_object(mod) { |c|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s =   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s |= c.public\_instance\_methods(false).select{ |m| m.to\_s =~ /=$/  
}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s |= c.private\_instance\_methods(false).select{ |m| m.to\_s =~ /=$/  
}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s |= c.protected\_instance\_methods(false).select{ |m| m.to\_s =~  
/=$/ }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.reject!{ |e| e !~ /\w/ }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eqmeths[c.name] = s unless s.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;}  
&nbsp;&nbsp;puts  
&nbsp;&nbsp;y eqmeths

> **···**
>
> ---  
> YAML::PrivateType:  
> &nbsp;&nbsp;- type\_id=  
> &nbsp;&nbsp;- value=  
> YAML::SpecialHash:  
> &nbsp;&nbsp;- default=  
> YAML::Syck::BadAlias:  
> &nbsp;&nbsp;- name=  
> YAML::BaseEmitter:  
> &nbsp;&nbsp;- options=  
> YAML::Stream:  
> &nbsp;&nbsp;- documents=  
> &nbsp;&nbsp;- options=  
> Process::GID:  
> &nbsp;&nbsp;- eid=  
> YAML::Syck::PrivateType:  
> &nbsp;&nbsp;- type\_id=  
> &nbsp;&nbsp;- value=  
> YAML::Syck::DomainType:  
> &nbsp;&nbsp;- type\_id=  
> &nbsp;&nbsp;- value=  
> &nbsp;&nbsp;- domain=  
> YAML::YPath:  
> &nbsp;&nbsp;- segments=  
> &nbsp;&nbsp;- flags=  
> &nbsp;&nbsp;- predicates=  
> Dir:  
> &nbsp;&nbsp;- pos=  
> YAML::Syck::Parser:  
> &nbsp;&nbsp;- options=  
> Thread:  
> &nbsp;&nbsp;- priority=  
> &nbsp;&nbsp;- abort\_on\_exception=  
> Hash:  
> &nbsp;&nbsp;- default=  
> Process:  
> &nbsp;&nbsp;- gid=  
> &nbsp;&nbsp;- egid=  
> &nbsp;&nbsp;- uid=  
> &nbsp;&nbsp;- maxgroups=  
> &nbsp;&nbsp;- euid=  
> &nbsp;&nbsp;- groups=  
> Struct::Tms:  
> &nbsp;&nbsp;- stime=  
> &nbsp;&nbsp;- utime=  
> &nbsp;&nbsp;- cstime=  
> &nbsp;&nbsp;- cutime=  
> Process::UID:  
> &nbsp;&nbsp;- eid=  
> YAML::DomainType:  
> &nbsp;&nbsp;- type\_id=  
> &nbsp;&nbsp;- value=  
> &nbsp;&nbsp;- domain=  
> YAML::Syck::Node:  
> &nbsp;&nbsp;- type\_id=  
> &nbsp;&nbsp;- value=  
> &nbsp;&nbsp;- anchor=  
> &nbsp;&nbsp;- kind=  
> IO:  
> &nbsp;&nbsp;- lineno=  
> &nbsp;&nbsp;- sync=  
> &nbsp;&nbsp;- pos=  
> YAML::Syck::Loader:  
> &nbsp;&nbsp;- bufsize=  
> &nbsp;&nbsp;- private\_types=  
> &nbsp;&nbsp;- families=  
> &nbsp;&nbsp;- anchors=
> 
> T.

---

<div class="post-metadata">

**Author:** ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)\
**Post date:** [7 October 2005 02:26 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/15 "2005-10-07T02:26:50Z")

</div>

"Eric Mahurin" \<eric\_mahurin@yahoo.com\> wrote in message

> But, I think the self.foo=value syntax would still be preferred  
> by most. I'm not sure why you dislike it so much to propose  
> making such radical changes.

I would be happy if all self calls required self.foo, or if self was  
uniformly optional.

It's just that  
&nbsp;&nbsp;&nbsp;bar  
&nbsp;&nbsp;&nbsp;baz  
&nbsp;&nbsp;&nbsp;self.foo=  
&nbsp;&nbsp;&nbsp;foo

leaves the third line sticking out to me. Perhaps very much about personal  
preference.

> We are also talking about inside the class, so you know the

It affects other places as well, such as instance\_eval.

p.s. I didn't mean to gripe, I love the language 🙂

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [6 October 2005 14:20 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/16 "2005-10-06T14:20:38Z")

</div>

Hi --

> **···**
>
> On Thu, 6 Oct 2005, James Edward Gray II wrote:
> 
> > On Oct 6, 2005, at 8:51 AM, Gavin Kistner wrote:
> > 
> > > On Oct 6, 2005, at 1:31 AM, itsme213 wrote:
> > > 
> > > > I guess a set\_x(val) is the other alternative. Not sure which I dislike more  
> > > > ☹
> > > 
> > > I'm amazed that you find either the above or foo(x) more appealing than simply using self.foo = x. In my personal opinion, the assignment is clear, and made clearer by the presence of 'self'.
> > 
> > [snip]
> > 
> > > But...really, I'm reasonably happy with the way things are now.
> > 
> > I believe I have only ever run into this problem when using instance\_eval(). Is it more pervasive than that?
> 
> It's always the case that
> 
> &nbsp;&nbsp;&nbsp;bareword = \<anything\>
> 
> is parsed as a local variable assignment, not a method call. It's  
> really not a big deal, though -- it's a small price to pay for all the  
> times we don't have to use an explicit 'self' receiver.
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [6 October 2005 15:14 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/17 "2005-10-06T15:14:14Z")

</div>

Hi --

> i think a symbol for local vars makes a TON of sense and is, really, in  
> keeping with ruby's design since every other scope is signaled by a symbol.

That ("in keeping with Ruby's design") is only true if you assume that  
the non-local variables somehow represent the truth about Ruby's  
design in a way that the local ones do not. I would suggest, however,  
that the "bareword" nature of local variables is 100% as much part of  
Ruby's design as the $var nature of globals, etc., is.

The thing is, it doesn't have to be a "winner-take-all" contest  
between two abstractions (with symbol/without symbol). It's a system;  
it can have components that differ from each other, and/or are similar  
to each other, in any number of ways.

David

> **···**
>
> On Thu, 6 Oct 2005, Ara.T.Howard wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)\
**Post date:** [7 October 2005 02:26 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/18 "2005-10-07T02:26:49Z")

</div>

"Ara.T.Howard" \<Ara.T.Howard@noaa.gov\> wrote in message

news:Pine.LNX.4.62.0510060818160.2730@harp.ngdc.noaa.gov...

> it's just my opinion, but i think this is clear as a whistle:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;color 'blue'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width 42

It is clear. But by munging writer and reader into a single method, it makes  
some kinds of manipulations of the methods difficult (e.g. programatically  
generating synchronization based on readers and writers).

---

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [7 October 2005 03:43 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/19 "2005-10-07T03:43:29Z")

</div>

Using instance\_eval breaks encapsulation anyways (you have full  
access), so I don't see much differnce from self.foo= and  
@foo=.

Personally, I think using instance\_eval regularly is bad  
practice since it breaks encapsulation. My guess is that many  
times it is done so that some code doesn't have a specify a  
receiver:

foo.bar {  
&nbsp;&nbsp;abc(1,2,3)  
&nbsp;&nbsp;xyz(4,5,6)  
}

where #bar did an instance eval on the block using some object  
in foo (including foo itself). I'd say this is better:

foo.bar { |o|  
&nbsp;&nbsp;o.abc(1,2,3)  
&nbsp;&nbsp;o.xyz(4,5,6)  
}

where #bar simply yielded the object in foo to the block.

A little more typing, but you don't break encapsulation and you  
don't have to question what "self" is in your blocks.

> **···**
>
> --- itsme213 \<itsme213@hotmail.com\> wrote:
> 
> > \> We are also talking about inside the class, so you know the
> > 
> > It affects other places as well, such as instance\_eval.
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [6 October 2005 15:28 UTC](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363/20 "2005-10-06T15:28:10Z")

</div>

You're right, of course.

I was thinking of how Ruby cleverly distinguishes local variables from method calls (described on page 329 of the Pickaxe2) and confused myself. Sorry for the noise.

James Edward Gray II

> **···**
>
> On Oct 6, 2005, at 9:20 AM, David A. Black wrote:
> 
> > It's always the case that
> > 
> > &nbsp;&nbsp;bareword = \<anything\>
> > 
> > is parsed as a local variable assignment, not a method call.

[Next page](https://rubytalk.org/t/foo-the-only-exception-to-the-implicit-self-rule/21363.md?page=2)
