Class: BMI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ BMI

Returns a new instance of BMI.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bmi/bmi.rb', line 5

def initialize(data={})    
  @data = data
  
  @data = {
    :weight_k     => 0,
    :height_m     => 0,
    :weight_p     => 0,
    :height_i     => 0
  }

  @errors = {
      :Exists     =>  false,
      :Missing    =>  "Error: You must put a weight and a height !",
      :Mismatch   =>  "Error: Arguments must be Numbers !",
      :Amount     =>  "Error: Two diferent arguments expected",
      :Height     =>  "Error: Use 'm'|'M' for 'meters' or 'i'|'I' for 'inches'",
      :Weight     =>  "Error: Use 'k'|'K' for 'kilograms' or 'p'|'P' for 'pounds'",
      :Wrong      =>  "Use '-h' or '--help' for more help"
    }
 
  @errtype=[]
  @imperial = false
  @metric   = false
  @weight   = 0
  @height   = 0
  @bmi      = 0
  @prime    = 0
  @percent  = 0
end

Instance Attribute Details

#bmiObject

Returns the value of attribute bmi.



3
4
5
# File 'lib/bmi/bmi.rb', line 3

def bmi
  @bmi
end

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/bmi/bmi.rb', line 3

def data
  @data
end

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/bmi/bmi.rb', line 3

def errors
  @errors
end

#errtypeObject

Returns the value of attribute errtype.



3
4
5
# File 'lib/bmi/bmi.rb', line 3

def errtype
  @errtype
end

#imperialObject

Returns the value of attribute imperial.



3
4
5
# File 'lib/bmi/bmi.rb', line 3

def imperial
  @imperial
end

#metricObject

Returns the value of attribute metric.



3
4
5
# File 'lib/bmi/bmi.rb', line 3

def metric
  @metric
end

#percentObject

Returns the value of attribute percent.



3
4
5
# File 'lib/bmi/bmi.rb', line 3

def percent
  @percent
end

Instance Method Details

#calcObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/bmi/bmi.rb', line 172

def calc
    if determinates == "Imperial" 
      puts "Your body mass index is: "+get_imperial.to_i.to_s
      puts "You are "+get_prime
      puts "Your status is :"+cases
      return true
    end
    if determinates == "Metric"
      puts "Your body mass index is: "+get_metric.to_i.to_s
      puts "You are "+get_prime
      puts "Your status is :"+cases
      return true
    end
    return  get_errors if error
    return false unless @data.select{|k,v| v.to_f > 0}.count > 0
end

#casesObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/bmi/bmi.rb', line 150

def cases
  if @bmi > 0
  case
    when @bmi < 18.5
      msj= "Underweight"
    when @bmi.between?(18.5,25)
      msj= "Normal"
    when @bmi.between?(25,30)
      msj= "Overweight"
    when @bmi.between?(30,35)
      msj= "Obese Class I"
    when @bmi.between?(35,40)
      msj= "Obese Class II"
    when @bmi > 40
      msj= "Obese Class III"
  end
  return msj
  else
    return false
  end
end

#determinatesObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bmi/bmi.rb', line 75

def determinates
  @metric=@imperial=false if error
  data = @data.select{|k,v| v.to_f > 0}
  @imperial = true if data[:weight_p] && data[:height_i]
  @metric   = true if data[:weight_k] && data[:height_m]
  @weight = data[:weight_k] || data[:weight_p]  
  @height = data[:height_m] || data[:height_i]  
  @weight = @weight.to_f
  @height = @height.to_f
  return "Imperial" if @imperial
  return "Metric" if @metric
  return "None"  if @imperial == @metric
end

#errorObject



115
116
117
# File 'lib/bmi/bmi.rb', line 115

def error
  return @errors[:Exists]
end

#get_errorsObject



119
120
121
122
123
124
125
126
# File 'lib/bmi/bmi.rb', line 119

def get_errors
  @errtype.uniq!
  display = []
  @errtype.each do |k|
  display<<@errors[k]
  end
  return display.shift
end

#get_imperialObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bmi/bmi.rb', line 89

def get_imperial
  if determinates == "Imperial"
  @bmi =( ( @weight ) / ( @height * @height ) ) * 703
  return @bmi
  elsif @weight == 0 || @height == 0
    @errors[:Exists]=true
    @errtype<<:Amount
    return false
  else
    return false
  end
end

#get_metricObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bmi/bmi.rb', line 102

def get_metric
  if determinates == "Metric"
  @bmi =( @weight ) / ( @height * @height )
  return @bmi
  elsif @weight == 0 || @height == 0
    @errors[:Exists]=true
    @errtype<<:Amount
    return false
  else
    return false
  end
end

#get_primeObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bmi/bmi.rb', line 128

def get_prime
  if @bmi > 0 
    @prime = ( @bmi / 25 )
    prti = @prime.to_s.match(/\d+/)
    ceros = @prime.to_s.match(/\.(0+)/)
    if !ceros
      ceros = []
      ceros[1] = "0"
    end
    prcnt = @prime.to_s.match(/\.(\d{1,2})/)
    if prti[0].to_i <= 0
      @percent = (100)*(ceros[1].length) - (prcnt[1].to_i)
    return @percent.to_s+"% under your ideal weight"
    else
      @percent = prcnt[1].to_i + (100*(prti[0].to_i-1)) 
    return @percent.to_s+"% over your ideal weight"
    end
  else
    return false
  end
end

#height(height, type = "m") ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bmi/bmi.rb', line 37

def height(height, type="m")
  @data[:height_m]=@data[:height_i]=0
  @errors[:Exists] = false
  @errtype=[]
  unless type.to_s.match(/m|i|M|I/) && type.class==String
    @errors[:Exists]=true
    @errtype<<:Height
  end
  if height.to_s.match(/\d/) && !@errors[:Exists]
    @data[:height_m]=height if type == "m" || type == "M"
    @data[:height_i]=height if type == "i" || type == "I"
  elsif !height.to_s.match(/\d/)
    @errors[:Exists]=true
    @errtype<<:Mismatch
  end

  return !@errors[:Exists]
end

#weight(weight, type = "k") ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bmi/bmi.rb', line 56

def weight(weight, type="k")
  @data[:weight_k]=@data[:weight_p]=0
  @errors[:Exists] = false
  @errtype=[]
  unless type.to_s.match(/k|p|K|P/) && type.class==String
    @errors[:Exists]=true
    @errtype<<:Weight
  end
  if weight.to_s.match(/\d/) && !@errors[:Exists]
    @data[:weight_k]=weight if type == "k" || type == "K"
    @data[:weight_p]=weight if type == "p" || type == "P"
  elsif !weight.to_s.match(/\d/)
    @errors[:Exists]=true
    @errtype<<:Mismatch
  end

  return !@errors[:Exists]
end