Forum: Ruby Re: Mixins and variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1c1e3bdfe006a22214102fcd6434a012?d=identicon&s=25 daniels (Guest)
on 2005-12-07 06:18
(Received via mailing list)
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.
#####################################################################################
06c1bab0fb222c7426c02887cd728936?d=identicon&s=25 johannes.friestad (Guest)
on 2005-12-07 06:30
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-12-07 06:38
(Received via mailing list)
Hi --

On Wed, 7 Dec 2005, Johannes Friestad 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
dblack@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!
06c1bab0fb222c7426c02887cd728936?d=identicon&s=25 johannes.friestad (Guest)
on 2005-12-07 06:43
(Received via mailing list)
Thanks.

jf
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 gwtmp01 (Guest)
on 2005-12-07 06:51
(Received via mailing list)
On Dec 7, 2005, at 12:29 AM, Johannes Friestad 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
for more details (section 4.3)
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 florgro (Guest)
on 2005-12-07 15:12
(Received via mailing list)
gwtmp01@mac.com 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.
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 gwtmp01 (Guest)
on 2005-12-07 16:26
(Received via mailing list)
On Dec 7, 2005, at 9:09 AM, Florian GroƟ wrote:

> gwtmp01@mac.com 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...
This topic is locked and can not be replied to.