modules as namespace

J

Jim

i am writing plugins for sketchup and use a module to encapsulate and
protect my variable and methods from name clashes.

Is there any difference between the following?

module A
def A.say_hi # or self.say
puts "hi"
end
end

module B; end
def B.say_hi
puts "hi"
end

thanks.
 
D

David A. Black

Hi --

i am writing plugins for sketchup and use a module to encapsulate and
protect my variable and methods from name clashes.

Is there any difference between the following?

module A
def A.say_hi # or self.say
puts "hi"
end
end

module B; end
def B.say_hi
puts "hi"
end

The only difference I'm aware of has to do with constant resolution:

H = "Hi from top level"

module A
H = "Hi from A"
def A.say_hi
puts H
end
end

module B
H = "Hi from B"
end

def B.say_hi
puts H
end

A.say_hi # Hi from A
B.say_hi # Hi from top level


David

--
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Ruby/Rails training, mentoring, consulting, code-review
Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2)

September Ruby training in NJ has been POSTPONED. Details to follow.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top