Extensions for String
Extensions for String
Extensions for String
Extensions for String
Extensions for String
Extensions for String
Extensions for String
Extensions for String
Simple transformation to CamelCase from snake_case
@example
'foo_bar'.camel_case # => 'FooBar'
# File lib/ramaze/snippets/string/camel_case.rb, line 14 14: def camel_case 15: split('_').map{|e| e.capitalize}.join 16: end
Compatibility with 1.9
# File lib/ramaze/snippets/string/end_with.rb, line 10 10: def end_with?(other) 11: other = other.to_s 12: self[-other.size, other.size] == other 13: end
String#escape is an extensible escaping mechanism for string. currently it suports
'<div>foo bar</div>'.esc(:html) 'foo bar'.esc(:uri) 'foo bar'.esc(:cgi)
# File lib/ramaze/snippets/string/esc.rb, line 13 13: def escape which = :html 14: case which 15: when :html 16: Rack::Utils.escape_html(self) 17: when :cgi 18: Rack::Utils.escape(self) 19: when :uri 20: ::URI.escape(self) 21: else 22: raise ArgumentError, "do not know how to escape '#{ which }'" 23: end 24: end
compatibility with Ruby 1.9
# File lib/ramaze/snippets/string/ord.rb, line 10 10: def ord 11: self[0] 12: end
convert to snake_case from CamelCase
@example
'FooBar'.snake_case # => 'foo_bar'
# File lib/ramaze/snippets/string/snake_case.rb, line 13 13: def snake_case 14: gsub(/\B[A-Z][^A-Z]/, '_\&').downcase.gsub(' ', '_') 15: end
Compatibility with 1.9
# File lib/ramaze/snippets/string/start_with.rb, line 7 7: def start_with?(other) 8: other = other.to_s 9: self[0, other.size] == other 10: end
Useful for writing indented String and unindent on demand, based on the first line with indentation.
# File lib/ramaze/snippets/string/unindent.rb, line 7 7: def unindent 8: find_indent = proc{ |l| l.find{|l| !l.strip.empty?}.to_s[/^(\s+)/, 1] } 9: 10: lines = self.split("\n") 11: space = find_indent[lines] 12: space = find_indent[lines.reverse] unless space 13: 14: strip.gsub(/^#{space}/, '') 15: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.