Skip to content

Instantly share code, notes, and snippets.

@deepak
Created December 12, 2010 11:58
Show Gist options
  • Select an option

  • Save deepak/737997 to your computer and use it in GitHub Desktop.

Select an option

Save deepak/737997 to your computer and use it in GitHub Desktop.
testing how extending self behaves and module_function
#!/usr/bin/env ruby
# USAGE:
# irb -rself_extend.rb
# > reload
require 'test/unit'
def reload
puts "Loading: #{__FILE__} ..."
load __FILE__
end
class Object
def metaclass
class << self; self; end
end
end
module Say
def bark
'barking'
end
def self.name
"Say"
end
end
module SayMyself
def bark
'barking'
end
def self.name
"SayMyself"
end
extend self
end
# sanitized version, cleans up after itself.
# makes a copy of the instance method
# as a class method and makes the instance method private
module CleanSay
def bark
'barking'
end
module_function :bark
end
class Dog
include Say
end
class JavaDog
extend Say
end
class PoliceDog
include CleanSay
# the benifit of "module_function" is that we can call it as
# CleanSay.bark and for convenience can include in a class and the
# class can call it as a private instance method
def saw(obj=:criminal)
bark if :criminal == obj
end
end
class TestSelfExtend < Test::Unit::TestCase
def test_say
assert_equal true, Say.instance_methods(false).include?("bark")
assert_equal false, Say.private_instance_methods(false).include?("bark")
assert_equal true, Say.metaclass.instance_methods(false).include?("name")
assert_equal false, Say.instance_eval { self.respond_to? :bark }
assert_equal false, Say.class_eval { self.respond_to? :bark }
end
def test_say_myself
# NOTE: namespace-pollution, "bark" method is in two places and is public in both
assert_equal true, SayMyself.instance_methods(false).include?("bark")
assert_equal false, SayMyself.private_instance_methods(false).include?("bark")
assert_equal true, SayMyself.metaclass.instance_methods(false).include?("bark")
assert_equal true, SayMyself.instance_eval { self.respond_to? :bark }
assert_equal true, SayMyself.class_eval { self.respond_to? :bark }
end
def test_clean_say
assert_equal false, CleanSay.instance_methods(false).include?("bark")
assert_equal true, CleanSay.private_instance_methods(false).include?("bark")
assert_equal true, CleanSay.metaclass.instance_methods(false).include?("bark")
# NOTE: namespace-pollution, "bark" method is in two places and is public in both
assert_equal true, CleanSay.instance_eval { self.respond_to? :bark }
assert_equal true, CleanSay.class_eval { self.respond_to? :bark }
end
def test_dog
assert_equal true, Dog.new.respond_to?(:bark)
assert_equal true, JavaDog.respond_to?(:bark)
assert_equal false, PoliceDog.new.respond_to?(:bark)
assert_equal true, PoliceDog.private_instance_methods.include?('bark')
# thanks to My_Hearing, banisterfiend on #ruby irc
# respond_to?, method_defined? etc works on public api, that's how 'respond_to? :bark' returns false
# even though the function runs
assert_equal false, PoliceDog.new.instance_eval { respond_to? :bark }
# defined? "bark" is different than defined? bark
assert_equal "method", PoliceDog.new.instance_eval { defined? bark }
assert_equal 'barking', PoliceDog.new.instance_eval { bark rescue nil }
assert_equal true, PoliceDog::CleanSay.respond_to?(:bark)
end
end
__END__
SOURCE:
http://www.neeraj.name/2009/10/20/extending-self-in-a-module.html
http://www.railsfire.com/article/extending-self-module
You should rather use module_function instead of extend self
by Anonymous - 2009-10-21 16:43
Instance methods of modules are only available if the module is included somewhere. If you include the module into its own
singleton class (via extend), then I think it's safe to assume the modules methods use no facilities of the object it is
attached to (doesn't refer to self or any instance variables). In other words, it pretends to be a function.
Since such a (pseudo-) function is of no use for the user of an object, it'll just pollute the namespace. That can be avoided
by making the method private. But you can't do that if you extend self - the method would be private there too,
YourModule.method would raise a NoMethodError.
The clean solution to it is module_function. It makes your method available via a public YourModule.foo and makes the
instance method private.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment