lib/ethon/extensions/string.rb in ethon-0.2.0 vs lib/ethon/extensions/string.rb in ethon-0.3.0
- old
+ new
@@ -3,14 +3,36 @@
# This module contains all core extensions ethon
# needs.
module Extensions
module String # :nodoc:
- # Return part of the string.
+ unless ''.respond_to?(:byteslice)
+ # Return part of the string.
+ #
+ # @return [ String ] Part of the string.
+ def byteslice(*args)
+ self[*args]
+ end
+ end
+
+ # Convert string to underscore. Taken from
+ # activesupport, renamed in order to no collide
+ # with it.
#
- # @return [ String ] Part of the string.
- def byteslice(*args)
- self[*args]
+ # @example Underscore.
+ # "ClassName".underscr
+ # #=> "class_name"
+ #
+ # @return [ String ] Underscore string.
+ def underscr
+ word = self.dup
+ word.gsub!('::', '_')
+ word.gsub!(/(?:([A-Za-z\d])|^)(#{/(?=a)b/})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
+ word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
+ word.tr!("-", "_")
+ word.downcase!
+ word
end
end
end
end