[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

why does block know about one instance variable and not the other?

anne001

7/9/2006 2:22:00 PM

I can't find what the problem is with @foundinfo: since it is defined
outside a block, the block should find it no? @item is defined outside
the second block and that second block finds it. What is @item and
@foundinfo different?

Do I have a typo I don't see, or it there a principle of the thing?

Here is my simplified code with debugging puts

class Linear
@foundinfo=Hash.new
p " 1. debugging @foundinfo created"
p @foundinfo.class #this is ok

def run

data="hello dud"
data.split(/</).each do |line|
line =~ /d(.)d/
@item=$1
p "2. debugging, @foundinfo found?"
p @foundinfo.class # this is not ok, @foundinfo not found

lines="and so on and some more"
lines.split(/</).each do |chunk|
p "3. debugging, @item found?"
p @item #this is ok, @item found
p "3. debugging, @foundinfo found?"
p @foundinfo.class # this is still not ok, @foundinfo
not found
end # end do chunk
end # end do line
end # end run

end # end class

a=Linear.new
a.run

5 Answers

dblack

7/9/2006 2:55:00 PM

0

anne001

7/9/2006 11:41:00 PM

0

Thank you for your response.

It did occured to me sometime after I wrote the post that the
difference between the two instance variables was that one was outside
the define and the other inside. A look at
http://www.ruby-doc.org/docs/UsersGuide/rg/instanc...
and other examples, shows that the instance variable is always defined
in a method without saying why.

So I was very glad to find your response.

I thought of an instance as a copy of the class. so implicitely I
assumed it would get a copy of an instance variable. Maybe I should
think of an object as an inheritance of methods: So it inherits
methods, not variables.

But if that was the rule, it would not work for constants either
http://www.ruby-doc.org/docs/UsersGuide/rg/cons...
in the example there, it would get a method and have no idea what the
constants were.

your example helped clarify what is going on.

class C
puts self
@var = 1
puts self
def x
puts self
@var = 2
end
end
p "create new instance of C"
y= C.new
p y
p "run method which creates an instance variable"
y.x
p y

class D
puts self
@@var = 1
puts self
def x
puts self
@@var = 2
end
end
p "create new instance of D"
y= D.new
p y
p "run method which creates an instance variable"
y.x
p y

In class C, running x adds @var to the object, but in D, running x does
not add @@var.

@var is an instance variable that belongs to whatever object is
"self" at that moment in runtime.

QUESTION: If that is the case, why does puts self not append @var=1 to
object C the way it
appends @var=2 to the instance?
#<C:0x25b58 @var=2>

In any case, the code in C outside of the methods is only run once when
the new method is called, and the object is C. OK. I had not thought of
it that way.

So is this an ok way to think about objects:
1. Objects inherit methods
2. The variable environment includes, read access to Class constants,
r/w to @@var and r/w to global variables
2. @var become local to whatever object is self at the moment they are
created.

dblack

7/10/2006 12:07:00 AM

0

Marcin Mielzynski

7/10/2006 12:53:00 AM

0

anne001 wrote:
>
> I thought of an instance as a copy of the class. so implicitely I
> assumed it would get a copy of an instance variable. Maybe I should
> think of an object as an inheritance of methods: So it inherits
> methods, not variables.
>

Instance of a class is not a copy of it. Classes are somewhat templates
for instances, they define what methods and variables their instances
will have. But since a Class in Ruby is also an object it can have
instance variable as well!.

>
> In class C, running x adds @var to the object,

As it should do

but in D, running x does
> not add @@var.
@@var is a special variable that is shared between class and it's
instances (so there is only one @@var, no matter how many instances
there are).

>
> @var is an instance variable that belongs to whatever object is
> "self" at that moment in runtime.

No, self is different in class context and instance context.

>
> QUESTION: If that is the case, why does puts self not append @var=1 to
> object C the way it
> appends @var=2 to the instance?
> #<C:0x25b58 @var=2>

Class objects have different default implementation of inspect method,
so they are printed differently using p. There is a way to query object
about their variables:

p C.instance_variable_get("@var")
-> 1
p y.instance_variable_get("@var")
-> 2


> In any case, the code in C outside of the methods is only run once when
> the new method is called, and the object is C. OK. I had not thought of
> it that way.
>
> So is this an ok way to think about objects:
> 1. Objects inherit methods

Nope, classes define methods which in turn will be available to their
instances upon construction

> 2. The variable environment includes, read access to Class constants,
> r/w to @@var and r/w to global variables
Again, depends on the context (receiver aka self)

> 2. @var become local to whatever object is self at the moment they are
> created.

I've never considered instance variables to be local to their instances
- but in a way, yes.

lopex

dblack

7/10/2006 1:03:00 AM

0