Skip to content

Commit

Permalink
Enable Ruby core extensions and String#camelcase.
Browse files Browse the repository at this point in the history
Extensions to the Ruby core classes live in the rake/core_ext directory
and are enabled by updating rake/core_extensions.rb to require them.
The actual extension consists of two parts, both in the rake/core_ext
directory.

Part One is a file named after the Ruby core class that you intend to
extend, for example rake/core_ext/string.rb. It's responsibility is to
require all files implementing extensions to a particular core Ruby
class.

Part Two is a subdirectory named after the Ruby core class you're extending.
For example, rake/core_ext/string/. This subdirectory holds all the files
that actually extend the core class named by the subdirectory. Use multiple
files to liberally modularize your extension code.

Finally, bringing everything together for use by the project rakefile, the
rake/core_extensions.rb file requires all Ruby core extensions you've
provided in Part One and Two.

Confusing? Don't let the above wording slow you down one bit. Check out the
code for the String#camelcase extension as managed by the following files:

 * rakefile.rb
 * rake/core_extensions.rb
 * rake/core_ext/string.rb
 * rake/core_ext/string/conversions.rb
  • Loading branch information
jonforums committed Jul 28, 2010
1 parent bec1be0 commit 282e918
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions rake/core_ext/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'rake/core_ext/string/conversions'
9 changes: 9 additions & 0 deletions rake/core_ext/string/conversions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class String
# Converts a lower case and underscored string to UpperCamelCase.
#
# Examples:
# "ruby_build_path".camelcase # => "RubyBuildPath"
def camelcase
self.gsub(/(?:\A|_)(.)/) { $1.upcase }
end
end
1 change: 1 addition & 0 deletions rake/core_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'rake/core_ext/string'
3 changes: 3 additions & 0 deletions rakefile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
require 'rake'
end

# Add extensions to core Ruby classes
require 'rake/core_extensions'

# Added download task from buildr
require 'rake/downloadtask'
require 'rake/extracttask'
Expand Down

0 comments on commit 282e918

Please sign in to comment.