Class: String

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

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter_in_uppercase = true) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/string.rb', line 80

def camelize(first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    self.to_s[0].chr.downcase + camelize(self)[1..-1]
  end
end

#camelize!(first_letter_in_uppercase = true) ⇒ Object



88
89
90
91
# File 'lib/string.rb', line 88

def camelize!(first_letter_in_uppercase = true)
  self.replace self.camelize(first_letter_in_uppercase)
  self
end

#is_number? ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/string.rb', line 61

def is_number?
  self.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
end

#nl2br ⇒ Object

Zamienia znak nowej linii na
'a



42
43
44
# File 'lib/string.rb', line 42

def nl2br
  self.gsub("\n\r","<br>").gsub("\r", "").gsub("\n", "<br />")
end

#nl2br! ⇒ Object



46
47
48
# File 'lib/string.rb', line 46

def nl2br!
  self.replace self.nl2br
end

#pick_byte ⇒ Object

Return a random byte of self. "Ruby rules".pick_byte #=> 121



185
186
187
# File 'lib/rand.rb', line 185

def pick_byte
  self[pick_index]
end

#pick_byte! ⇒ Object

Destructive pick_byte. Delete a random byte of self and return it. s = "Ruby rules" s.pick_byte! #=> 121 s #=> "Rub rules"



211
212
213
# File 'lib/rand.rb', line 211

def pick_byte!
  pick_char![0]
end

#pick_char ⇒ Object

Return a single-character string of a random character in self. "Ruby rules".pick_char #=> "y"



191
192
193
# File 'lib/rand.rb', line 191

def pick_char
  pick_byte.chr
end

#pick_char! ⇒ Object

Destructive pick_char. Delete a random character of the string and return it as a single-character string. s = "Ruby rules" s.pick_char! #=> "y" s #=> "Rub rules"



200
201
202
203
204
205
# File 'lib/rand.rb', line 200

def pick_char!
  i = pick_index
  rv = self[i,1]
  self[i,1] = ""
  rv
end

#pick_index ⇒ Object

Return a random byte index of self. "Ruby rules".pick_index #=> 3



217
218
219
# File 'lib/rand.rb', line 217

def pick_index
  rand(size)
end

#pick_index! ⇒ Object

Destructive pick_index. Delete a random byte of self and return it's index. s = "Ruby rules" s.pick_index #=> 3 s #=> "Rub rules"



226
227
228
229
230
# File 'lib/rand.rb', line 226

def pick_index!
  i = pick_index
  self[i,1] = ""
  i
end

#sanitize(options = {}) ⇒ Object



8
9
10
# File 'lib/string.rb', line 8

def sanitize(options={})
  ActionController::Base.helpers.sanitize(self, options)
end

#sanitize!(options = {}) ⇒ Object



12
13
14
# File 'lib/string.rb', line 12

def sanitize!(options={})
  self.replace self.sanitize(options)
end

#shuffle_chars ⇒ Object

Return the string with characters arranged in random order. "Ruby rules".shuffle_chars #=> "e lybRsuur"



170
171
172
# File 'lib/rand.rb', line 170

def shuffle_chars
  split(//).shuffle.join('')
end

#shuffle_chars! ⇒ Object

Destructive shuffle_chars. Arrange the characters of the string in new, random order. s = "Ruby rules".shuffle_chars s.shuffle_chars! s #=> "e lybRsuur"



179
180
181
# File 'lib/rand.rb', line 179

def shuffle_chars!
  self[0,size] = shuffle_chars
end

#strip_tags ⇒ Object



16
17
18
# File 'lib/string.rb', line 16

def strip_tags
  ActionView::Helpers::SanitizeHelper.strip_tags(self)
end

#strip_tags! ⇒ Object



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

def strip_tags!
  self.replace strip_tags
end

#to_class ⇒ Object

Allows you turning a string into a class if class with provided name exists Examples: "Fixnum".to_class #=> Fixnum "Something".to_class #=> nil



69
70
71
72
73
74
75
76
77
78
# File 'lib/string.rb', line 69

def to_class
  chain = self.split "::"
  klass = Kernel
  chain.each do |klass_string|
    klass = klass.const_get klass_string
  end
  klass.is_a?(Class) ? klass : nil
rescue NameError
  nil
end

#to_url ⇒ Object

Zwraca strina bez polskich literek, z małej litery, z usuniętymi innymi znakami oraz z zamienioną spacją na pauze (-)



26
27
28
29
30
31
32
33
34
# File 'lib/string.rb', line 26

def to_url
  temp = self.to_slug.transliterate.to_s.downcase
  temp.gsub!(/[^a-zA-Z 0-9]/, "")
  temp.gsub!(/\s/,'-')
  temp.gsub!(/\-+$/,'')
  temp.gsub!(/^\-+/,'')
  temp.gsub!(/\-{2,}/, '-')
  temp
end

#to_url! ⇒ Object

to_url tylko wykonane na sobie



37
38
39
# File 'lib/string.rb', line 37

def to_url!
  self.replace self.to_url
end

#underlinize ⇒ Object

Zamiana CamelCase na underline



51
52
53
# File 'lib/string.rb', line 51

def underlinize
  self.split(/(?=[A-Z])/).join('_').downcase.gsub("::_", "/")
end

#underlinize! ⇒ Object

Zamiana CamelCase na underline na sobie



56
57
58
59
# File 'lib/string.rb', line 56

def underlinize!
  self.replace self.underlinize
  self
end