Permalink
Browse files

DRYer

  • Loading branch information...
1 parent 4a25047 commit 9c0c08e930d47fa75c7a5533c0e6f3a9e667bcfc @banister committed Aug 24, 2010
Showing with 71 additions and 68 deletions.
  1. +23 −13 Rakefile
  2. +48 −55 lib/object2module.rb
View
36 Rakefile
@@ -1,6 +1,7 @@
# Rakefile added by John Mair (banisterfiend)
-require 'rake/gempackagetask'
+ require 'rake/gempackagetask'
+ require 'rake/rdoctask'
require 'rake/clean'
require 'lib/object2module/version.rb'
@@ -17,6 +18,7 @@ def apply_spec_defaults(s)
s.version = Object2module::VERSION
s.author = "John Mair (banisterfiend)"
s.email = 'jrmair@gmail.com'
+ s.has_rdoc = true
s.date = Time.now.strftime '%Y-%m-%d'
s.require_path = 'lib'
s.homepage = "http://banisterfiend.wordpress.com"
@@ -26,25 +28,33 @@ end
# common tasks
task :compile => :clean
-# spec = Gem::Specification.new do |s|
-# apply_spec_defaults(s)
-# s.platform = 'i386-mswin32'
-# s.files = ["Rakefile", "README", "LICENSE", "lib/object2module.rb", "lib/1.8/object2module.#{dlext}", "lib/1.9/object2module.#{dlext}", "lib/object2module/version.rb"]
-# end
+spec = Gem::Specification.new do |s|
+ apply_spec_defaults(s)
+ s.platform = 'i386-mingw32'
+ s.files = ["Rakefile", "README", "LICENSE",
+ "lib/object2module.rb", "lib/1.8/object2module.#{dlext}",
+ "lib/1.9/object2module.#{dlext}", "lib/object2module/version.rb", "test/test_object2module.rb"]
+end
-spec = Gem::Specification.new do |s|
- apply_spec_defaults(s)
- s.platform = Gem::Platform::RUBY
- s.extensions = FileList["ext/**/extconf.rb"]
- s.files = ["Rakefile", "README", "LICENSE", "lib/object2module.rb", "lib/object2module/version.rb"] +
- FileList["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c"].to_a
-end
+# spec = Gem::Specification.new do |s|
+# apply_spec_defaults(s)
+# s.platform = Gem::Platform::RUBY
+# s.extensions = FileList["ext/**/extconf.rb"]
+# s.files = ["Rakefile", "README", "LICENSE", "lib/object2module.rb", "lib/object2module/version.rb", "test/test_object2module.rb"] +
+# FileList["ext/**/extconf.rb", "ext/**/*.h", "ext/**/*.c"].to_a
+# end
Rake::GemPackageTask.new(spec) do |pkg|
pkg.need_zip = false
pkg.need_tar = false
end
+
+
+Rake::RDocTask.new do |rd|
+ rd.main = "README.rdoc"
+ rd.rdoc_files.include("README.rdoc", "lib/object2module.rb")
+end
View
103 lib/object2module.rb
@@ -16,78 +16,71 @@
end
-# call-seq:
-# obj.gen_extend(other, ...) => obj
-
-# Adds to _obj_ the instance methods from each object given as a
-# parameter.
-
-# class C
-# def hello
-# "Hello from C.\n"
-# end
-# end
-
-# class Klass
-# def hello
-# "Hello from Klass.\n"
-# end
-# end
-
-# k = Klass.new
-# k.hello #=> "Hello from Klass.\n"
-# k.gen_extend(C) #=> #<Klass:0x401b3bc8>
-# k.hello #=> "Hello from C.\n"
class Object
- def gen_extend(*objs)
+ def __gen_extend_or_include__(extend_or_include, *objs) #:nodoc:
raise ArgumentError, "wrong number of arguments (0 for 1)" if objs.empty?
objs.each { |o|
begin
mod = o.__to_module__
- extend(mod)
+ send(extend_or_include, mod)
ensure
mod.__reset_tbls__ if mod != o &&o != Object && o != Class && o != Module
end
}
self
end
-end
-
-# call-seq:
-# gen_include(other, ...) => self
+ # call-seq:
+ # obj.gen_extend(other, ...) => obj
+ #
+ # Adds to _obj_ the instance methods from each object given as a
+ # parameter.
+ #
+ # class C
+ # def hello
+ # "Hello from C.\n"
+ # end
+ # end
+ #
+ # class Klass
+ # def hello
+ # "Hello from Klass.\n"
+ # end
+ # end
+ #
+ # k = Klass.new
+ # k.hello #=> "Hello from Klass.\n"
+ # k.gen_extend(C) #=> #<Klass:0x401b3bc8>
+ # k.hello #=> "Hello from C.\n"
+ def gen_extend(*objs)
+ __gen_extend_or_include__(:extend, *objs)
+ end
+end
-# Adds to the implied receiver the instance methods from each object given as a
-# parameter.
-
-# class C
-# def hello
-# "Hello from C.\n"
-# end
-# end
-
-# class Klass
-# gen_include(C)
-# end
-
-# k = Klass.new
-# k.hello #=> "Hello from C.\n"
class Module
+
+ # call-seq:
+ # gen_include(other, ...) => self
+ #
+ # Adds to the implied receiver the instance methods from each object given as a
+ # parameter.
+ #
+ # class C
+ # def hello
+ # "Hello from C.\n"
+ # end
+ # end
+ #
+ # class Klass
+ # gen_include(C)
+ # end
+ #
+ # k = Klass.new
+ # k.hello #=> "Hello from C.\n"
def gen_include(*objs)
- raise ArgumentError, "wrong number of arguments (0 for 1)" if objs.empty?
-
- objs.each { |o|
- begin
- mod = o.__to_module__
- include(mod)
- ensure
- mod.__reset_tbls__ if mod != o && o != Object && o != Class && o != Module
- end
- }
-
- self
+ __gen_extend_or_include__(:include, *objs)
end
end

0 comments on commit 9c0c08e

Please sign in to comment.