Scopes

Hi!

I’ve a question about scopes:

@mail=Someclass.new
@pass=Someclass.new
def @pass.check

how to acces @mail here?

end

thx
Gergo
±[Kontra, Gergely @ Budapest University of Technology and Economics]-+

    Email: kgergely@mcl.hu,  kgergely@turul.eet.bme.hu          |

URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |
±------“Olyan langesz vagyok, hogy poroltoval kellene jarnom!”-------+
.
Magyar php mirror es magyar php dokumentacio: http://hu.php.net

Kontra, Gergely wrote:

Hi!

I’ve a question about scopes:

@mail=Someclass.new
@pass=Someclass.new
def @pass.check

how to acces @mail here?

end

The instance variables are always private in Ruby:
you can’t access the instance variables of an object from
a different object.

Regards, Christian

I’ve a question about scopes:

@mail=Someclass.new
@pass=Someclass.new
def @pass.check

how to acces @mail here?

end

The instance variables are always private in Ruby:
you can’t access the instance variables of an object from
a different object.

What if I write mail and pass instead of @mail and @pass
Gergo

±[Kontra, Gergely @ Budapest University of Technology and Economics]-+

    Email: kgergely@mcl.hu,  kgergely@turul.eet.bme.hu          |

URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |
±------“Olyan langesz vagyok, hogy poroltoval kellene jarnom!”-------+
.
Magyar php mirror es magyar php dokumentacio: http://hu.php.net

Christian Szegedy szegedy@nospam.or.uni-bonn.de writes:

Kontra, Gergely wrote:

Hi!
I’ve a question about scopes:

@mail=Someclass.new

@pass=Someclass.new
def @pass.check

how to acces @mail here?

end

The instance variables are always private in Ruby:
you can’t access the instance variables of an object from
a different object.

Except in his example above they’re the same object.

@a = 123
def fred
puts @a
end

fred #=> 123

Cheers

Dave

“Kontra, Gergely” wrote in

The instance variables are always private in Ruby:
you can’t access the instance variables of an object from
a different object.

What if I write mail and pass instead of @mail and @pass
Gergo

This does not work either - you have to transport'' @mail as an argument into the @pass scope. However, a localmail’‘
could be seen been in a @pass'' singleton method defined viadefine_method’’.

···

class Foo
def initialize
@pass = Object.new
end
def mail=(got)
mail = got
class << @pass ;self end.send(:define_method,:check)
{ || (mail =~ /milk/) ? “yes” : “no” }
got
end
def got_milk?
p @pass.check
end
end

a,b = Foo.new, Foo.new

a.mail = "books"
b.mail = “milk and cookies”

a.got_milk? # no
b.got_milk? # yes

/Christoph

“Kontra, Gergely” wrote in

What if I write mail and pass instead of @mail and @pass

Sorry I miss read your email - yes this is possible with the
``define_method’’ technique illustrated in my previous reply.

/Christoph