Class: String

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.hex(args = { :length => 24, :case => :upper }) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/hosemonkey/ext/string.rb', line 32

def hex(args = { :length => 24, :case => :upper })
  unless args.include_only?( :length, :case )
    raise "Invalid parameters in random"
  end
  
  h = (((0..args[:length]).map{rand(256).chr}*"").unpack("H*")[0][0,args[:length]])
  
  if args[:case] == :lower
    h.downcase
  else
    h.upcase
  end
end

.random(args = { :length => 24, :charset => :all }) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hosemonkey/ext/string.rb', line 46

def random(args = { :length => 24, :charset => :all })
  unless args.include_only?( :length, :charset )
    raise "Invalid parameters in random"
  end 

  if args[:charset] == :alpha
    chars = ('a'..'z').to_a + ('A'..'Z').to_a
  elsif args[:charset] == :alnum_upper
    chars = ('A'..'Z').to_a + ('0'..'9').to_a
  else
    chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
  end
  (0...args[:length]).collect { chars[Kernel.rand(chars.length)] }.join  
end

Instance Method Details

#camelizeObject

def uncamelize

uncameled = self.each_char.inject("") do |result,c|
  unless "#{c}" =~ /^[A-Z]$/
    result << "#{c}"
  else
    result << "_" unless result.empty?
    result << "#{c}".downcase
  end
end
uncameled

end



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hosemonkey/ext/string.rb', line 14

def camelize
  cap_next_char = true
  cameled = self.each_char.inject("") do |result, c|
    unless "#{c}" =~ /^[-_]$/
      unless cap_next_char
        result << "#{c}"
      else
        result << "#{c}".upcase
        cap_next_char = false
      end
    else
      cap_next_char = true
    end
    result
  end
end