Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > attr_* position in a class and their results

Reply
Thread Tools

attr_* position in a class and their results

 
 
Arno J.
Guest
Posts: n/a
 
      08-14-2007
Hello,
I was playing with instance variables when I made up those two classes :

class Vartest_before
def init
@instance_variable = "I belong to the instance"
end

attr_accessor :instance_variable

def instance_variable
@instance_variable = "Forced"
end
end

class Vartest_after
def init
@instance_variable = "I belong to the instance"
end

def instance_variable
@instance_variable = "Forced"
end

attr_accessor :instance_variable
end

Now when using them, here are the results (# =>) :

a = Vartest_after.new
a.init
b = Vartest_before.new
b.init

puts a.instance_variable # => I belong to the instance
a.instance_variable = "Changed"
puts a.instance_variable # => Changed

puts b.instance_variable # => Forced
b.instance_variable = "Changed"
puts b.instance_variable # => Forced


I don't understand what's happening :/ Can you ?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      08-14-2007
Alle marted=C3=AC 14 agosto 2007, Arno J. ha scritto:
> Hello,
> I was playing with instance variables when I made up those two classes :
>
> class Vartest_before
> def init
> @instance_variable =3D "I belong to the instance"
> end
>
> attr_accessor :instance_variable
>
> def instance_variable
> @instance_variable =3D "Forced"
> end
> end
>
> class Vartest_after
> def init
> @instance_variable =3D "I belong to the instance"
> end
>
> def instance_variable
> @instance_variable =3D "Forced"
> end
>
> attr_accessor :instance_variable
> end
>
> Now when using them, here are the results (# =3D>) :
>
> a =3D Vartest_after.new
> a.init
> b =3D Vartest_before.new
> b.init
>
> puts a.instance_variable # =3D> I belong to the instance
> a.instance_variable =3D "Changed"
> puts a.instance_variable # =3D> Changed
>
> puts b.instance_variable # =3D> Forced
> b.instance_variable =3D "Changed"
> puts b.instance_variable # =3D> Forced
>
>
> I don't understand what's happening :/ Can you ?



In the case of Vartest_before, when you call=20

puts b.instance_variable

the value of @instance_variable is changed to 'Forced' before being returne=
d.=20
If you substitute that line with

puts b.instance_variable_get(:@instance_variable)

you'd get what (I think) you expect, that is 'Changed'. This doesn't happen=
=20
for the other class because there your definition of instance_variable is=20
overwritten by the one provided by attr_accessor.

I hope this helps

Stefano

 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      08-14-2007
Arno J. wrote:
> class Vartest_before
> def init
> @instance_variable = "I belong to the instance"
> end
>
> attr_accessor :instance_variable


This defines a method instance_variable

> def instance_variable
> @instance_variable = "Forced"
> end
> end


This defines the method instance_variable again. The version created by
attr_accessor doesn't exist anymore.

> class Vartest_after
> def init
> @instance_variable = "I belong to the instance"
> end
>
> def instance_variable
> @instance_variable = "Forced"
> end
>
> attr_accessor :instance_variable
> end


This time it's the other way around: First you define the method with
def, then you redefine it with attr_accessor. It's like doing
def bla()
1
end
def bla()
2
end
Everytime you call bla, it will return 2 because the second definition
overrides the first one.


HTH,
Sebastian
--
NP: Metallica - Trapped Under Ice
Jabber: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
Arno J.
Guest
Posts: n/a
 
      08-14-2007
Ok, I'm must be tired because I was expecting just the opposite, which
wasn't logical.
Sorry for this dumb question.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-14-2007
On 14.08.2007 21:34, Arno J. wrote:
> Ok, I'm must be tired because I was expecting just the opposite, which
> wasn't logical.


Maybe it was even caused by using a different order in class definition
and usage. This got me confused initially as well.

> Sorry for this dumb question.


No prob.

Kind regards

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If Brit police are terrified their jobs are threatened by cutbacks,this isn't the way to illustrate their value RichA Digital Photography 62 01-12-2011 01:10 AM
Prophets are honored by everyone, except the people of their hometownand their own family. Eljee Digital Photography 8 09-21-2006 05:35 AM
Since MSN CHAT went pay per use. Is their any other free ones out their Hugh Computer Support 8 05-19-2004 05:52 PM
What the pros use to power their flashes... and their digital cameras. Dan Sullivan Digital Photography 21 01-04-2004 04:40 PM
Stop Spammers by Hitting Their Servers - Not Their Email. Magic347 Computer Support 27 07-03-2003 04:36 PM



Advertisments