Skip to content

Instantly share code, notes, and snippets.

@marcric
Created September 20, 2009 17:53
Show Gist options
  • Save marcric/189861 to your computer and use it in GitHub Desktop.
Save marcric/189861 to your computer and use it in GitHub Desktop.
# scope_dbl_deny.rb
=begin
This code comes from a discussion in rails-br group.
http://groups.google.com/group/rails-br/browse_thread/thread/775784b8e759721f?hl=pt-BR
It is all about two newbie questions:
One about scope operator "::".
And other about double deny "!!".
There is something about metaprogramming in the thread also.
=end
puts '== scope operator =='
module Foo
def self.do_something
::Bar.hi
end
module Bar
def self.hi
puts "not here..."
end
end
end
module Bar
def self.hi
puts "I'm here!!!"
end
end
Foo.do_something
puts '======='
class Test
def ola
puts "Hello!!"
end
end
t = Test.new
t::ola
puts '======='
module Foo
def self.do_something
::Bar.hi
end
module Bar
def self.hi
puts "not here..."
end
end
end
module Bar
def self.hi
puts "I'm here!!!"
end
def self.sleep
puts "zzZZZZ"
end
end
Foo.do_something # to aqui!!
Foo::Bar.hi # aqui nao..
# Foo::Bar.sleep # <<<< undefined method
puts '======='
module Foo
def self.test
puts 'Foo message'
end
end
module Bar
module Foo
def self.test
puts 'Bar::Foo message'
end
end
def self.internal_test
Foo.test
end
def self.external_test
::Foo.test
end
end
Bar.internal_test
Bar.external_test
puts ''
puts '== double deny =='
def is_it_true?
true
end
puts is_it_true?
puts !is_it_true?
puts !!is_it_true?
puts '======='
# When it doesn't make any sense, William Molinari comes to help
def is_it_true?
nil
end
puts is_it_true?
puts !is_it_true?
puts !!is_it_true?
puts '======='
module Guitar
end
module Blues
module Sax
end
def self.has_guitar?; !!defined? Guitar; end
def self.has_sax?; !!defined? Sax; end
end
module Metal
def self.has_guitar?; !!defined? Guitar; end
def self.has_sax?; !!defined? Sax; end
end
puts Blues.has_guitar? # => true
puts Blues.has_sax? # => true
puts Metal.has_guitar? # => true
puts Metal.has_sax? # => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment