Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string_extensions.rb,
lib/midiator/drivers/dls_synth.rb

Overview

Some extensions to the built-in Ruby String class.

Authors

Copyright © 2008 Ben Bleything, except where noted.

This code released under the terms of the MIT license.

Instance Method Summary collapse

Instance Method Details

#camelizeObject

NOTE: Stolen from ActiveSupport. They hold the copyright. Our modifications are making it a method on String and removing the lowerCamelCase option since we don’t use it.

camelize converts strings to CamelCase.

camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.

Examples

"active_record".camelize #=> "ActiveRecord"
"active_record/errors".camelize #=> "ActiveRecord::Errors"


30
31
32
33
34
35
36
37
# File 'lib/string_extensions.rb', line 30

def camelize
  return self.gsub( /\/(.?)/ ) {
    "::" + $1.upcase
  }.
  gsub( /(^|_)(.)/ ) {
    $2.upcase
  }
end

#to_bytesObject



18
19
20
21
22
23
24
25
# File 'lib/midiator/drivers/dls_synth.rb', line 18

def to_bytes
  bytes = 0
  self.each_byte do |byte|
    bytes <<= 8
    bytes += byte
  end
  return bytes
end

#underscoreObject

NOTE: Stolen from ActiveSupport. They hold the copyright. The only modifications were to make it a String instance method instead of a function.

The reverse of camelize. Makes an underscored form from the expression in the string.

Changes ‘::’ to ‘/’ to convert namespaces to paths.

Examples

"ActiveRecord".underscore #=> "active_record"
"ActiveRecord::Errors".underscore #=> active_record/errors


51
52
53
54
55
56
57
# File 'lib/string_extensions.rb', line 51

def underscore
  return self.gsub( /::/, '/' ).
  gsub( /([A-Z]+)([A-Z][a-z])/, '\1_\2' ).
  gsub( /([a-z\d])([A-Z])/    , '\1_\2' ).
  tr( "-", "_" ).
  downcase
end