PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Scope of class instance variables?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Scope of class instance variables?

Réponse
 
LinkBack Outils de la discussion
Vieux 29/03/2008, 16h37   #1
Ruby Freak
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Scope of class instance variables?


I am reading Hal Fulton's "The Ruby Way" On page 57 he makes the
statement:
"Class instance variables cannot be referenced from within instance
methods and, in general are not very useful"

huh?..
This , in my feeble mind, contradicts everything else I have ever
read, except that the example he gives is similar to the @y = 7
example below, and in that case, @y is not available to the accessor
or any other method that I have played with.

In the following code, the original assignment of @x = 7 and @y = 5
don't seem to do anything, even with the attr_accessor. I know that
both are read by the compiler as I can assign @y = 5/0 and get a
division by zero error.

So, could someone please explain why the accessor for @y does not work
here. If I hand write a reader and writer for @y, it works just fine.
There are different scopes here, but I had the impression that the
accessor would break down that barrier and make @y available
throughout the class just as the initialize method is able to access
the class variable @x and assign the passed in value. That value is
then available to the @x accessor.

I thought for a while that the @y accessor might work in the singleton
class of Myclass, but it doesn't.

class Myclass
attr_accessor :y, :x

@x = 7
@y = 5

def initialize(new_val= l)
@x = new_val ? new_val : 0
end
end

mc = Myclass.new(3)

puts mc.y #=> Nil
puts mc.x #-> 3

I know I can make the @x and @y class variables available to instance
methods by referencing them inside defined methods and the scope seem
to be class wide, but it just seems like the first assignments above
should work as written.

Thanks in advance










  Réponse avec citation
Vieux 29/03/2008, 17h09   #2
Trans
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Scope of class instance variables?



On Mar 29, 11:40 am, Ruby Freak <twscann...@gmail.com> wrote:
> I am reading Hal Fulton's "The Ruby Way" On page 57 he makes the
> statement:
> "Class instance variables cannot be referenced from within instance
> methods and, in general are not very useful"


I think that just an out-dated way of looking at things. I expect Hal
would agree that needs to be updated to current trends.

I remember once, Hal and I got in a big argument about floating-point
arithmetic. A tad spiteful I pointed out a flaw in his book. Despite
the heated argument, he took it quite gracefully, saying he was always
happy to receive comments/corrections. That was pretty damn
respectable, IMHO. If he doesn't see this thread, write him directly.

T.

  Réponse avec citation
Vieux 29/03/2008, 17h36   #3
surreal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Scope of class instance variables?

[Note: parts of this message were removed to make it a legal post.]

unsuscribe

2008/3/29, Trans <transfire@gmail.com>:
>
>
>
> On Mar 29, 11:40 am, Ruby Freak <twscann...@gmail.com> wrote:
> > I am reading Hal Fulton's "The Ruby Way" On page 57 he makes the
> > statement:
> > "Class instance variables cannot be referenced from within instance
> > methods and, in general are not very useful"

>
>
> I think that just an out-dated way of looking at things. I expect Hal
> would agree that needs to be updated to current trends.
>
> I remember once, Hal and I got in a big argument about floating-point
> arithmetic. A tad spiteful I pointed out a flaw in his book. Despite
> the heated argument, he took it quite gracefully, saying he was always
> happy to receive comments/corrections. That was pretty damn
> respectable, IMHO. If he doesn't see this thread, write him directly.
>
>
> T.
>
>



--
Harshad Joshi

  Réponse avec citation
Vieux 29/03/2008, 17h39   #4
surreal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Scope of class instance variables?

[Note: parts of this message were removed to make it a legal post.]

unsubscribe

2008/3/29, surreal <firewalrus@gmail.com>:
>
> unsuscribe
>
> 2008/3/29, Trans <transfire@gmail.com>:
> >
> >
> >
> > On Mar 29, 11:40 am, Ruby Freak <twscann...@gmail.com> wrote:
> > > I am reading Hal Fulton's "The Ruby Way" On page 57 he makes the
> > > statement:
> > > "Class instance variables cannot be referenced from within instance
> > > methods and, in general are not very useful"

> >
> >
> > I think that just an out-dated way of looking at things. I expect Hal
> > would agree that needs to be updated to current trends.
> >
> > I remember once, Hal and I got in a big argument about floating-point
> > arithmetic. A tad spiteful I pointed out a flaw in his book. Despite
> > the heated argument, he took it quite gracefully, saying he was always
> > happy to receive comments/corrections. That was pretty damn
> > respectable, IMHO. If he doesn't see this thread, write him directly.
> >
> >
> > T.
> >
> >

>
>
> --
> Harshad Joshi





--
Harshad Joshi

  Réponse avec citation
Vieux 29/03/2008, 17h48   #5
Ruby Freak
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Scope of class instance variables?

On Mar 29, 9:39 am, surreal <firewal...@gmail.com> wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> unsubscribe
>
> 2008/3/29, surreal <firewal...@gmail.com>:
>
>
>
>
>
> > unsuscribe

>
> > 2008/3/29, Trans <transf...@gmail.com>:

>
> > > On Mar 29, 11:40 am, Ruby Freak <twscann...@gmail.com> wrote:
> > > > I am reading Hal Fulton's "The Ruby Way" On page 57 he makes the
> > > > statement:
> > > > "Class instance variables cannot be referenced from within instance
> > > > methods and, in general are not very useful"

>
> > > I think that just an out-dated way of looking at things. I expect Hal
> > > would agree that needs to be updated to current trends.

>
> > > I remember once, Hal and I got in a big argument about floating-point
> > > arithmetic. A tad spiteful I pointed out a flaw in his book. Despite
> > > the heated argument, he took it quite gracefully, saying he was always
> > > happy to receive comments/corrections. That was pretty damn
> > > respectable, IMHO. If he doesn't see this thread, write him directly.

>
> > > T.

>
> > --
> > Harshad Joshi

>
> --
> Harshad Joshi


Thanks Trans.

I guess Harshad didn't like my post! :-)

Anyway, I would still appreciate an explanation for the difference in
scope, or the reason the first assignment doesn't work.

Thanks
  Réponse avec citation
Vieux 29/03/2008, 22h52   #6
Robert Dober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Scope of class instance variables?

On Sat, Mar 29, 2008 at 4:40 PM, Ruby Freak <twscannell@gmail.com> wrote:
>
> I am reading Hal Fulton's "The Ruby Way" On page 57 he makes the
> statement:
> "Class instance variables cannot be referenced from within instance
> methods and, in general are not very useful"
>
> huh?..
> This , in my feeble mind, contradicts everything else I have ever
> read, except that the example he gives is similar to the @y = 7
> example below, and in that case, @y is not available to the accessor
> or any other method that I have played with.
>
> In the following code, the original assignment of @x = 7 and @y = 5
> don't seem to do anything, even with the attr_accessor. I know that
> both are read by the compiler as I can assign @y = 5/0 and get a
> division by zero error.
>
> So, could someone please explain why the accessor for @y does not work
> here. If I hand write a reader and writer for @y, it works just fine.
> There are different scopes here, but I had the impression that the
> accessor would break down that barrier and make @y available
> throughout the class just as the initialize method is able to access
> the class variable @x and assign the passed in value. That value is
> then available to the @x accessor.
>
> I thought for a while that the @y accessor might work in the singleton
> class of Myclass, but it doesn't.
>
> class Myclass

### make this
class << self
> attr_accessor :y, :x

end

Now this is an accessor to the singleton class of the class where the
class instance variables are stored (that is not completely correct,
maybe I shall say from where one has access to them).
>
> @x = 7
> @y = 5
>
> def initialize(new_val= l)
> @x = new_val ? new_val : 0

# and this shall read
self.class.x = new_val || 0
> end
> end
>
>
> puts mc.y #=> Nil

Myclass.x --> 7
mc = Myclass::new( 42 )
Myclass.x --> 42
mc = Myclass::new 101010
Myclass.x --> 101010
there is of course no mc.x but please see below.

I do however fear that you confuse instance variables with class
instance variables.
The former exist on an per object base and the later on a per class
base ( a class being an object of course ).
For completeness I'll show you how to use the former

class A
attr_accessor :a
def initialize; @a = 42 end
end
a= A.new
a.a --> 42
b = A.new
a.a = 101010
a.a -> 101010
b.a -> 42

HTH
Robert
--
http://ruby-smalltalk.blogspot.com/

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15911 seconds with 14 queries