Module: English::Style

Included in:
String
Defined in:
lib/gems/english-0.3.1/lib/english/style.rb

Overview

The Style mixin provides a means of applying common alteration patterns to strings.

This module is automatically mixed into the String class, but the Style mixin is designed in such a way that it can be mixed into any class that supports the #to_s method.

Examples

"SuperMan".snakecase   #=> "super_man"
"super_man".dasherize  #=> "super-man"

– TODO: Would like to provide a means for mixing into Symbol

so that Symbols are returned. Use #to_self(obj)?

TODO: With #pathize, is downcasing really needed? After all paths

can have capitalize letters ;p

TODO: With #methodize, is downcasing any but the first letter

really needed? Desipite Matz preference methods can have
capitalized letters.

++

Defined Under Namespace

Modules: Replace

Instance Method Summary collapse

Instance Method Details

#camelcaseObject

By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to “:lower” then camelize produces lowerCamelCase.

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

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


112
113
114
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 112

def camelcase
  to_s.gsub(/\/(.?)/){ "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/){ $1.upcase }
end

#capitalcaseObject

Upcase first letter.



62
63
64
65
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 62

def capitalcase
  str = to_s
  str[0,1].upcase + str[1..-1]
end

#capitalizeObject

Standard capitialize style.

NOTE: I hate this definition and think it

should be as capitalcase, but Matz
doesn't agree.


57
58
59
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 57

def capitalize
  to_s.capitalize
end

#demodulizeObject

Removes the module part from a modularized expression.

"English::Style".demodulize  #=> "Style"
"Style".demodulize           #=> "Style"


255
256
257
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 255

def demodulize
  to_s.gsub(/^.*::/, '')
end

#downcaseObject

Standard downcase style.



33
34
35
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 33

def downcase
  to_s.downcase
end

#dresnerObject

Dresener Obfuscation.

Scramble the inner characters of words leaving the text still readable (research at Cambridge University, code by KurtDresner).

For example, the above text may result in:

Srblamce the iennr cchrteaars of wodrs lvenaig the txet stlil rbeaadle
(rreceash at Cbamigdre Uverintisy, cdoe by KrneruestDr?)

:credit: Kurt Dresener



186
187
188
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 186

def dresner
  to_s.gsub(/\B\w+\B/){$&.split(//).sort_by{rand}}
end

#jumbleObject

Jumble string.



169
170
171
172
173
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 169

def jumble
  j = ''
  to_s.split(//).each_with_index{ |c,i| j << ( i % 2 == 0 ? c.downcase : c.upcase ) }
  j
end

#lowercaseObject

Same as downcase style.



38
39
40
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 38

def lowercase
  to_s.downcase
end

#methodizeObject

Converts a module name into a valid method name. This method is geared toward code reflection.

Examples

"SuperMan".methodize          #=> "super_man"
"SuperMan::Errors".methodize  #=> "super_man__errors
"MyModule::MyClass".methodize #=> "my_module__my_class"

See also #modulize, #pathize



223
224
225
226
227
228
229
230
231
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 223

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

#modulizeObject

Converts a pathized or methodized string into a valid ruby class or module name. This method is geared toward code reflection.

"camel_case".modulize          #=> "CamelCase"
"camel/case".modulize          #=> "Camel::Case"
"my_module__my_path".modulize  #=> "MyModule::MyPath"

See also #methodize, #pathize



242
243
244
245
246
247
248
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 242

def modulize
  to_s.
  gsub(/__(.?)/){ "::#{$1.upcase}" }.
  gsub(/\/(.?)/){ "::#{$1.upcase}" }.
  gsub(/(?:_+)([a-z])/){ $1.upcase }.
  gsub(/(^|\s+)([a-z])/){ $1 + $2.upcase }
end

#ordinalizeObject

Ordinalize turns a number string into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

'1'.ordinalize     # => "1st"
'2'.ordinalize     # => "2nd"
'1002'.ordinalize  # => "1002nd"
'1003'.ordinalize  # => "1003rd"


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 144

def ordinalize
  number_string = to_s
  if number_string =~ /\d{1,2}$/
    number = $1.to_i     
    if (11..13).include?(number.to_i % 100)
      r = "#{number}th"
    else
      r = case number.to_i % 10
        when 1; "#{number}st"
        when 2; "#{number}nd"
        when 3; "#{number}rd"
        else    "#{number}th"
      end
    end
    number_string.sub(/\d{1,2}$/, r)
  else
    number_string
  end
end

#pathizeObject

Converts a module or method name into a unix pathname. This method is geared toward code reflection.

"MyModule::MyClass".pathize    #=> my_module/my_class
"my_module__my_class".pathize  #=> my_module/my_class

TODO: Make sure that all scenarios return a valid unix path. TODO: Make sure it is revertible.

See also #modulize, #methodize



204
205
206
207
208
209
210
211
212
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 204

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

#snakecaseObject

The reverse of camelize. Converts a CamelCase word into an underscored form.

This also changes ‘::’ to ‘/’, usefule for converting namespaces to pathnames.

Examples

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


93
94
95
96
97
98
99
100
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 93

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

#subcamelcaseObject

Convert a snakecase phrase into a camelcase phrase, but making the first letter lowercase.

"active_record".camelcase               #=> "activeRecord"
"active_record/errors".camelcase        #=> "activeRecord::Errors"


128
129
130
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 128

def subcamelcase
  upper_camelcase.uncapitalize
end

#titlecaseObject

Convert a phrase into a title.

"this is a string".titlecase
=> "This Is A String"


79
80
81
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 79

def titlecase
  to_s.gsub(/\b\w/){$&.upcase}
end

#uncapitalizeObject Also known as: uncapitalcase

Downcase first letter.



68
69
70
71
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 68

def uncapitalize
  str = to_s
  str[0,1].downcase + str[1..-1]
end

#upcaseObject

Standard upcase style.



43
44
45
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 43

def upcase
  to_s.upcase
end

#uppercaseObject

Same as upcase style.



48
49
50
# File 'lib/gems/english-0.3.1/lib/english/style.rb', line 48

def uppercase
  to_s.upcase
end