Field Scope

F

forest

what is the scope of @run field in the code below. Is it module static
? If not, how module static methods can access it ?

module Test
module Unit
def self.run=(flag)
@run = flag
end

def self.run?
@run ||= false
end
end
end

at_exit do
unless $! || Test::Unit.run?
exit Test::Unit::AutoRunner.run($0 != "-e" && $0)
end
end
 
R

Ross Bamford

what is the scope of @run field in the code below. Is it module static
? If not, how module static methods can access it ?

module Test
module Unit
def self.run=(flag)
@run = flag
end

def self.run?
@run ||= false
end
end
end

Forget about static and all that, and remember that modules are objects
too. Test::Unit is just a constant, that references the Module instance
for that module.

Does this make it any clearer?

Test::Unit.run = 'Hmm...'
Test::Unit.instance_eval { @run } # => "Hmm..."

a = Test::Unit
a.instance_eval { @run } # => "Hmm..."
 
F

forest

Thanks a lot, it explains everything.

Funny that somewhere i read that ruby follows Least Surprise Paradigm,
but personally me i was deeply surprised several times, while learning
it :)
 
R

Ross Bamford

Thanks a lot, it explains everything.

Funny that somewhere i read that ruby follows Least Surprise Paradigm,
but personally me i was deeply surprised several times, while learning
it :)

I feel the same way, but I realise now that I feel that way because I
expect Ruby to be as convoluted and... well, surprising, as other
languages. It surprises me by how unsurprising it is (*most of the time*)
;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top