Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/just_share/string.rb

Instance Method Summary collapse

Instance Method Details

#capitalize_humanizedObject

Humanize the String format rails humanized (but renamed to not have conflicts & REFACTORED)



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/just_share/string.rb', line 3

def capitalize_humanized
  # Replace underscore to whitespace
  self.gsub!('_', ' ')

  # Split on where are the hyphens
  self_in_array = self.split(/-/)

  # Recreate Capitalizing
  it_new_format = ''
  self_in_array.each do |sub_str|
    it_new_format = it_new_format+sub_str.capitalize
  end

  # Return it new format
  it_new_format
end

#equals?(str) ⇒ Boolean

Helpful for the RSpec test

Returns:

  • (Boolean)


21
22
23
# File 'lib/just_share/string.rb', line 21

def equals? str
  self == str
end

#lastObject

Return it last char



26
27
28
# File 'lib/just_share/string.rb', line 26

def last
  self[self.length-1]
end

#to_constantObject

Rails Constantize (but renamed to not have conflicts)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/just_share/string.rb', line 31

def to_constant
  camel_cased_word = self
  names = camel_cased_word.split('::')

  # Trigger a builtin NameError exception including the ill-formed constant in the message.
  Object.const_get(camel_cased_word) if names.empty?

  # Remove the first blank element in case of '::ClassName' notation.
  names.shift if names.size > 1 && names.first.empty?

  names.inject(Object) do |constant, name|
    if constant == Object
      constant.const_get(name)
    else
      candidate = constant.const_get(name)
      next candidate if constant.const_defined?(name, false)
=begin TODO UNCOMMENT IF ANYTHING CRASH
      next candidate unless Object.const_defined?(name)

      # Go down the ancestors to check it it's owned
      # directly before we reach Object or the end of ancestors.
      constant = constant.ancestors.inject do |const, ancestor|
        break const    if ancestor == Object
        break ancestor if ancestor.const_defined?(name, false)
        const
      end

      # owner is in Object, so raise
      constant.const_get(name, false)
=end

    end
  end
end