Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

Hello, I am studying Ruby through RubyMonk. I have a doubt with this exercise: In this exercise, change push to return 10 as per A = 10 in the topmost level, outside the Kata module. module Kata A = 5 module Dojo B = 9 A = 7 class ScopeIn def push A end end end end A = 10 ::push.A = 10 puts "#{Kata::Dojo::ScopeIn.new.push}" Can someone give the solution?

OpenStudy (opcode):

This is a constant lookup exercise by the looks of it, so you should know the '::' is a constant lookup operator that looks up the array constant only in the perimeter module. So: ``` module Kata A = 5 module Dojo B = 9 A = 7 class ScopeIn def push ::A end end end end A = 10 ``` (Kata::Dojo::ScopeIn.new.push should return 10) Explanation: If you prepend a constant with '::' (without a parent, Kata and Dojo in this example), the scoping will happen on the topmost level. In our case the top most level is the \(A = 10\) class (notice it is the last line), and because \(A = 10\) is outside of the module Kata it is considered to be the 'highest class'. Summary of Explanation: ::A returns 10 since it scopes the topmost level. This is because the parents (Kata, and Dojo) are not present. Source: Ruby syntax cheatsheet: https://github.com/savini/cheatsheets/raw/master/ruby/RubyCheat.pdf

OpenStudy (anonymous):

Thank you Opcode. It has work!!! :D

OpenStudy (hari5719):

wow

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!