# Re: Mixins and variables

**URL:** <https://www.ruby-forum.com/t/re-mixins-and-variables/52667>\
**Category:** Ruby\
**Created:** [December 7, 2005, 6:18am UTC](https://www.ruby-forum.com/t/re-mixins-and-variables/52667 "2005-12-07T06:18:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 7, 2005, 6:18am UTC](https://www.ruby-forum.com/t/re-mixins-and-variables/52667/1 "2005-12-07T06:18:23Z")

</div>

It’s got nothing to do with mixins:

class TestClass  
def x=(arg)  
@x = arg  
end  
def x  
@x  
end  
def monkey(arg)  
x = arg  
end  
end

irb(main):012:0\> a = TestClass.new  
=\> #TestClass:0x2ac6e48  
irb(main):013:0\> a.monkey(‘aa’)  
=\> “aa”  
irb(main):014:0\> a.x  
=\> nil

In the monkey method, x = arg is assigning to the local variable x, not  
calling the method x=().

Refer to the x variable either as self.x or @x.

> from the object’s initialize().  
> end  
> x=alpha # x is set inside constructor

#####################################################################################  
This email has been scanned by MailMarshal, an email content filter.  
#####################################################################################

---

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 7, 2005, 6:30am UTC](https://www.ruby-forum.com/t/re-mixins-and-variables/52667/2 "2005-12-07T06:30:39Z")

</div>

OK, thanks. Can I force “x=(whatever)” to mean a method call instead  
of an assignment?  
I thought the parantheses were supposed to do that?

jf

---

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 7, 2005, 6:38am UTC](https://www.ruby-forum.com/t/re-mixins-and-variables/52667/3 "2005-12-07T06:38:56Z")

</div>

Hi –

On Wed, 7 Dec 2005, Johannes F. wrote:

> OK, thanks. Can I force “x=(whatever)” to mean a method call instead  
> of an assignment?
> 
> I thought the parantheses were supposed to do that?

You have to give the explicit receiver ‘self’. The thing is, local  
variable assignments can have things with parens on the right, so that  
alone is not enough to exempt that expression from being interpreted  
as a l.v. assignment.

## David

David A. Black  
removed\_email\_address@domain.invalid

“Ruby for Rails”, forthcoming from Manning Publications, April 2006!

---

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 7, 2005, 6:51am UTC](https://www.ruby-forum.com/t/re-mixins-and-variables/52667/4 "2005-12-07T06:51:12Z")

</div>

On Dec 7, 2005, at 12:29 AM, Johannes F. wrote:

> OK, thanks. Can I force “x=(whatever)” to mean a method call instead  
> of an assignment?  
> I thought the parantheses were supposed to do that?

It is a parsing problem. Because ruby supports zero argument methods  
the parser when it sees:

```
x = 4

```

can’t decide from the syntax alone if this is a local variable  
assignment  
or if this is a method call to the x= method for the object (with 0  
arguments).  
This decision needs to be made when the method is _parsed_ long before  
the method has actually been executed.

So the goal is to convince the parser of what you want. If you want  
this  
to be a method call then you have the following options:

```
self.x = 4	# explicit method call
x() = 4 # also explicit but not 'rubyish'

```

Putting arguments around right-hand-side expression doesn’t help the  
parser:

```
x = (4) # no help here

```

Neither does getting rid of spaces

```
x=(4) # still no help

```

The parser also learns as it goes along to help disambiguate other  
uses of the variable. See [http://dev.rubycentral.com/faq/rubyfaq-4.html](http://dev.rubycentral.com/faq/rubyfaq-4.html)  
for more details (section 4.3)

---

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 7, 2005, 6:43am UTC](https://www.ruby-forum.com/t/re-mixins-and-variables/52667/5 "2005-12-07T06:43:03Z")

</div>

Thanks.

jf

---

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 7, 2005, 3:12pm UTC](https://www.ruby-forum.com/t/re-mixins-and-variables/52667/6 "2005-12-07T15:12:48Z")

</div>

removed\_email\_address@domain.invalid wrote:

> ```
> self.x = 4 # explicit method call
> x() = 4 # also explicit but not 'rubyish'
> 
> ```

Hm, and a syntax error as well, if I’m not wrong.

---

<div class="post-metadata">

**Author:** ![daniels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@daniels](https://www.ruby-forum.com/u/daniels)\
**Post date:** [December 7, 2005, 4:26pm UTC](https://www.ruby-forum.com/t/re-mixins-and-variables/52667/7 "2005-12-07T16:26:23Z")

</div>

On Dec 7, 2005, at 9:09 AM, Florian Groß wrote:

> removed\_email\_address@domain.invalid wrote:
> 
> > ```
> > self.x = 4 # explicit method call
> > x() = 4 # also explicit but not 'rubyish'
> > 
> > ```
> 
> Hm, and a syntax error as well, if I’m not wrong.

My bad. I really should cut and paste into irb…
