-
-
Save 100lp/4740204 to your computer and use it in GitHub Desktop.
module Kata | |
A = 5 | |
module Dojo | |
B = 9 | |
A = 7 | |
class ScopeIn | |
def push | |
ScopIn::A # (why if i write just "::A" - it work too ?) | |
end | |
end | |
end | |
end | |
A = 10 | |
======================= | |
Kata::Dojo::ScopeIn.new.push should return 10 |
I may be late to this but here it goes:
If you read the question it tells you that "If you prepend a constant with :: WITHOUT A PARENT (meaning Dojo and Kata in this example), the scoping happens on the topmost level".
So in this case the top most level is the A=10 class (which is the last line).
A=10 is outside of the module Kata therefore it is the highest class.
Therefore:
::A returns 10 since it scopes the TOPMOST level because the parents (Dojo, Kata) are not present.
deeambler, thanks for that explanation. I was really frustrated trying to figure out what this question was asking.
Thanks for that answer deeambler. I was not understanding that at all... makes sense now.
irb(main):032:0> module Kata
irb(main):033:1> A = 5
irb(main):034:1> module Dojo
irb(main):035:2> B = 9
irb(main):036:2> A = 7
irb(main):037:2>
irb(main):038:2* class ScopeIn
irb(main):039:3> def push
irb(main):040:4> ::A
irb(main):041:4> end
irb(main):042:3> end
irb(main):043:2> end
irb(main):044:1> end
=> nil
irb(main):046:0* A = 10
=> 10
irb(main):047:0> A
=> 10
irb(main):048:0> Kata::A
=> 5
irb(main):049:0> Kata::Dojo::A
=> 7
irb(main):050:0> Kata::Dojo::ScopeIn.new.push
=> 10
irb(main):051:0> A = 8
=> 8
irb(main):052:0> Kata::Dojo::ScopeIn.new.push
=> 8
Big thanks to this, deeambler!!
::A returns 10, indeed. Rubymonk (where the task was taken from) doesn't let you pass, though.
@rookie , if you want to have the test pass.
module Kata
A = 5
module Dojo
B = 9
A = 7
class ScopeIn
def push
10
end
end
end
end
A = 10
I'm also stuck in this!