rubyhelper 0.2.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c821c362cc1520ecfca8057736a2822960fc33f8
4
- data.tar.gz: 1dae1f8ac6991d29c0cacdff681d7f991f140255
3
+ metadata.gz: c44208355c233532a223344b7f61ad48060b0e1e
4
+ data.tar.gz: f6080e470235dd504cc5c77f745d23694bdc5200
5
5
  SHA512:
6
- metadata.gz: 299aca8153165eb7a6d15ae623360484ccfa622d31153e993b55115d8eadfcf5c0e52848eadc51472a31e91bda9543dec987b64a390348f143b202c37a78654e
7
- data.tar.gz: f102e264cd1602530f42fb698299a8e83f01bc945fbf764b326663284957bab21b1ca0c161e0f5cf705e15f1c55077c87ca2982d381daeb47338d6f9dc8b2500
6
+ metadata.gz: 1a1bf0cba4dcbb66382590317ecbb0bc159dc8b48fb67a16ebeea18d9b63d25b2ff2451dc11f04faf0bdc1b07ba40ae851527608129907194e61206d30457a3e
7
+ data.tar.gz: 0fda05d5b7c5b9cc6fbeb6d15a218795e47d58c62e984e9d2839be43b8ab777f7ccfda6e1950c7e8f110b8877b097fe801b9532b8db0ce4418a56398b2ecb67c
@@ -2,24 +2,6 @@
2
2
 
3
3
  module ArrayHelper
4
4
 
5
- # A simple function like to_s(), but return a clean String
6
- # for exemple :
7
- # [1,2,3].to_clean_s(" ; ")
8
- # => "1 ; 2 ; 3"
9
- # ==Parameters:
10
- # sep:
11
- # A String to separe each element (or an Array, see to_clean_s_with_array)
12
- def to_clean_s(sep=' ')
13
- return to_clean_s_with_array(sep) if sep.is_a?Array
14
- to_clean_s_with_string(sep.to_s)
15
- end
16
-
17
- def to_clean_s_with_string sep
18
- str = String.new
19
- self.each{|e| str = str + e.to_s + sep.to_s}
20
- return str[0..(-sep.size - 1)]
21
- end
22
-
23
5
  # You can use an array as separator : [",", " "] will successively ',' and ' '
24
6
  def to_clean_s_with_array sep
25
7
  str = String.new
@@ -31,27 +13,38 @@ module ArrayHelper
31
13
  return str[0..(-sep.size - 1)]
32
14
  end
33
15
 
16
+ # Do the sum of an array of integer.
17
+ # if there is not integer, it will do a to_s.to_i of the element to try
18
+ # find an integer in the element. else, replace by a simple 0
34
19
  def sum
35
20
  return (self.size > 0) ? (self.map{|e| e.to_s.to_i}.reduce(:+)) : (0)
36
21
  end
37
22
 
23
+ # lke sum by with a to_f instead of to_i
38
24
  def sumf
39
25
  return (self.size > 0) ? (self.map{|e| e.to_s.to_f}.reduce(:+)) : (0.0)
40
26
  end
41
27
 
28
+ # Use the sum and divide by the size of the array. 0 if no element.
42
29
  def average
43
30
  return (self.size > 0) ? (self.sum / self.size) : (0)
44
31
  end
45
32
 
33
+ # Same than average but use to_f instead of to_i
46
34
  def averagef
47
35
  return (self.size > 0) ? (self.sumf / self.size.to_f) : (0.0)
48
36
  end
49
37
 
50
- #TODO benchmark with sort{|e| -e}
38
+ # get the n higher values of the array
51
39
  def maxs(n=1)
52
40
  return Array(self.sort[(-n)..(-1)])
53
41
  end
54
42
 
43
+ # get the n lower values of the array
44
+ def mins(n=1)
45
+ return Array(self.sort[0..(n-1)])
46
+ end
47
+
55
48
  end
56
49
 
57
50
  class Array
@@ -3,6 +3,7 @@
3
3
 
4
4
  module IntegerHelper
5
5
 
6
+ # get - or + function of the sign of the integer
6
7
  def sign
7
8
  return (self < 0) ? ("-") : ("+")
8
9
  end
@@ -5,35 +5,56 @@ require 'digest'
5
5
 
6
6
  module StringHelper
7
7
 
8
- def to_plain(case_mod = nil)
9
- string = self.tr(
10
- "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
11
- "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz")
8
+ # == Params
9
+ # case_mod: nil (not changement), :upcase, :capitalize or :downcase
10
+ # replace: if a char is not utf8 valid, character will replace it
11
+ # Remove accents from the string. Change the case as first argument
12
+ def to_plain(case_mod = nil, replace= " ")
13
+ s = self.encode("UTF-8", :invalid=>:replace, :replace=>replace).tr("ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
14
+ "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz")
15
+ return s.to_case(case_mod)
16
+ end
17
+
18
+ #to_plain alias
19
+ def p
20
+ return self.to_plain
21
+ end
22
+
23
+ # == Params
24
+ # case_mod: nil (not changement), :upcase, :capitalize or :downcase
25
+ # permit to do upcase/downcase/capitalize easier with a simple param
26
+ def to_case(case_mod = :downcase)
12
27
  case case_mod
13
28
  when :upcase
14
- return string.upcase
29
+ return self.upcase
15
30
  when :downcase
16
- return string.downcase
31
+ return self.downcase
32
+ when :capitalize
33
+ return self.capitalize
17
34
  else
18
- return string
35
+ return self
19
36
  end
20
37
  end
21
38
 
22
- def to_case(case_mod = :downcase)
23
- if case_mod == :upcase
24
- return self.to_plain.upcase
25
- else
26
- return self.to_plain.downcase
39
+ # == Params
40
+ # replace: a caracter to replace non-ascii chars
41
+ # case_mod: nil (not changement), :upcase, :capitalize or :downcase
42
+ # return a simple ascii string. Invalid characters will be replaced by "replace" (argument)
43
+ def to_ascii(replace="", case_mod = nil)
44
+ s = String.new
45
+ self.to_plain.each_char do |c|
46
+ s += ((c.ord > 255) ? (replace.to_s) : (c))
27
47
  end
48
+ return s.to_case(case_mod)
28
49
  end
29
50
 
30
- def to_ascii(case_mod = :upcase)
31
- return to_case(case_mod)
32
- end
33
-
51
+ # improvement of to_f to count "," caracter as "."
34
52
  def to_f
53
+ s = self.dup
35
54
  self.gsub!(',', '.')
36
- super
55
+ f = super
56
+ self.replace(s)
57
+ return f
37
58
  end
38
59
 
39
60
  #To_i with delimiter
@@ -55,7 +76,10 @@ module StringHelper
55
76
  Digest::SHA2.hexdigest(self)
56
77
  end
57
78
 
58
- #STATIC
79
+ # ==Param
80
+ # n: number of char
81
+ # char: char to replace if the initial str is too short
82
+ # Get a str with a static length
59
83
  def static(n, char=' ')
60
84
  if self.size < n
61
85
  return self + char * (n - self.size).to_i
@@ -64,12 +88,15 @@ module StringHelper
64
88
  end
65
89
  end
66
90
 
67
- #Returns true or false
91
+ #Returns true or false if the string if "true" or "false"
68
92
  def to_t
69
- if self == "true"
93
+ case self
94
+ when "true"
70
95
  return true
71
- else
96
+ when "false"
72
97
  return false
98
+ else
99
+ return nil
73
100
  end
74
101
  end
75
102
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyhelper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - poulet_a
@@ -50,6 +50,6 @@ rubyforge_project:
50
50
  rubygems_version: 2.4.1
51
51
  signing_key:
52
52
  specification_version: 4
53
- summary: to_int / float
53
+ summary: add to_plain
54
54
  test_files: []
55
55
  has_rdoc: