Skip to content

Instantly share code, notes, and snippets.

@100lp
Last active June 19, 2016 03:22
Show Gist options
  • Save 100lp/4740204 to your computer and use it in GitHub Desktop.
Save 100lp/4740204 to your computer and use it in GitHub Desktop.
If you prepend a constant with :: without a parent, the scoping happens on the topmost level. In this exercise, change push to return 10 as per A = 10 in the topmost level, outside the Kata module.module Kata
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
@Sanbornh
Copy link

deeambler, thanks for that explanation. I was really frustrated trying to figure out what this question was asking.

@growthcode
Copy link

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

@EdmundLeex
Copy link

Big thanks to this, deeambler!!

@rookie-
Copy link

rookie- commented Aug 22, 2015

::A returns 10, indeed. Rubymonk (where the task was taken from) doesn't let you pass, though.

@okram999
Copy link

okram999 commented Nov 8, 2015

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment