Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mixins and variables

0 views
Skip to first unread message

Johannes Friestad

unread,
Dec 7, 2005, 12:06:39 AM12/7/05
to
Hi,
I'm new to Ruby, and trying to figure out how the inheritance/mixin works:
I can't figure out how to set an instance variable with a mixin method
from the object's initialize().

Example:
-----------------
module TestMod
def x
@x
end
def x=(arg)
@x=arg
end
end

class TestClass
include TestMod
def initialize
x=('alpha')
printf("x=%s\n", x)
end
end

irb(main)..>tmp=TestClass.new
x=alpha # x is set inside constructor
=> #<TestClass:0x37d9520>
irb(main)..>tmp.x
=> nil # x is unset on the returned object
-----------------

Does anyone know why it doesn't work? What can I do instead?

--
Johannes Friestad
johannes...@gmail.com


Daniel Sheppard

unread,
Dec 7, 2005, 12:17:52 AM12/7/05
to
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.

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


Johannes Friestad

unread,
Dec 7, 2005, 12:29:12 AM12/7/05
to
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

dbl...@wobblini.net

unread,
Dec 7, 2005, 12:37:04 AM12/7/05
to
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
dbl...@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!


Daniel Sheppard

unread,
Dec 7, 2005, 12:38:02 AM12/7/05
to
self.x= will be a method call.

> -----Original Message-----
> From: Johannes Friestad [mailto:johannes...@gmail.com]
> Sent: Wednesday, 7 December 2005 4:29 PM
> To: ruby-talk ML

> Subject: Re: Mixins and variables
>
> 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?

#####################################################################################

Johannes Friestad

unread,
Dec 7, 2005, 12:41:01 AM12/7/05
to
Thanks.

jf


--
Johannes Friestad
johannes...@gmail.com


gwt...@mac.com

unread,
Dec 7, 2005, 12:49:21 AM12/7/05
to

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)


Florian Groß

unread,
Dec 7, 2005, 9:09:15 AM12/7/05
to
gwt...@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.

James Edward Gray II

unread,
Dec 7, 2005, 9:30:33 AM12/7/05
to
On Dec 6, 2005, at 11:06 PM, Johannes Friestad wrote:

> Hi,
> I'm new to Ruby, and trying to figure out how the inheritance/mixin
> works:
> I can't figure out how to set an instance variable with a mixin method
> from the object's initialize().

I see you have your answer, so let me just make some general comments:

> Example:
> -----------------
> module TestMod
> def x
> @x
> end
> def x=(arg)
> @x=arg
> end

You can replace the last six lines with:

attr_accessor :x

> end
>
> class TestClass
> include TestMod
> def initialize
> x=('alpha')

self.x = 'alpha' # as discussed, or just @x = 'alpha'

> printf("x=%s\n", x)

And we would usually write that as:

puts "x=#{x}"

> end
> end

Hope that gives you an idea or two.

James Edward Gray II

gwt...@mac.com

unread,
Dec 7, 2005, 10:25:56 AM12/7/05
to

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


0 new messages