Class: MathWizard

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

Class Method Summary collapse

Class Method Details

.abs_arr(arr) ⇒ Object



38
39
40
41
42
43
# File 'lib/math_wizard.rb', line 38

def self.abs_arr(arr)
  (0...arr.length).each do |i|
    arr[i] = arr[i].abs
  end
  arr
end

.add(*arr) ⇒ Object



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
34
35
36
# File 'lib/math_wizard.rb', line 8

def self.add(*arr)
  if arr[0].instance_of?(Array)
    if arr[0][0].instance_of?(Array)
      new_arr = []
      (0...arr[0].length).each do |i|
        new_arr.push(arr[0][i].sum)
      end
      new_arr
    elsif arr[0].instance_of?(Array)
      new_arr = []
      ind = 0
      (0...arr.length).each do |i|
        ind = arr[i].length if arr[i].length > ind
      end
      (0...ind).each do |i|
        sum = 0
        (0...arr.length).each do |j|
          sum += arr[j][i] if i < arr[j].length
        end
        new_arr.push(sum)
      end
      new_arr
    else
      arr[0].sum
    end
  else
    arr.sum
  end
end

.celsius(fahrenheit:) ⇒ Object



103
104
105
# File 'lib/math_wizard.rb', line 103

def self.celsius(fahrenheit:)
  (fahrenheit - 32) * (5 / 9)
end

.divide(*arr) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/math_wizard.rb', line 57

def self.divide(*arr)
  actual_arr = []
  if arr[0].instance_of?(Array)
    (0...arr[0].length).each do |i|
      actual_arr.push(arr[0][i] / arr[1][i])
    end
    actual_arr
  else
    arr[0] / arr[1]
  end
end

.fahrenheit(celsius:) ⇒ Object



99
100
101
# File 'lib/math_wizard.rb', line 99

def self.fahrenheit(celsius:)
  ((9 / 5) * celsius) + 32
end

.hiObject



4
5
6
# File 'lib/math_wizard.rb', line 4

def self.hi
  puts 'Hello world!'
end

.multiply(*arr) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/math_wizard.rb', line 69

def self.multiply(*arr)
  if arr[0].instance_of?(Array)
    if arr[0][0].instance_of?(Array)
      new_arr = []
      (0...arr[0].length).each do |i|
        new_arr.push(arr[0][i].inject(:*))
      end
      new_arr
    elsif arr[0].instance_of?(Array)
      new_arr = []
      ind = 0
      (0...arr.length).each do |i|
        ind = arr[i].length if arr[i].length > ind
      end
      (0...ind).each do |i|
        sum_value = 1
        (0...arr.length).each do |j|
          sum_value *= arr[j][i] if i < arr[j].length
        end
        new_arr.push(sum_value)
      end
      new_arr
    else
      arr[0].inject(:*)
    end
  else
    arr.inject(:*)
  end
end

.sub(*arr) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/math_wizard.rb', line 45

def self.sub(*arr)
  actual_arr = []
  if arr[0].instance_of?(Array)
    (0...arr[0].length).each do |i|
      actual_arr.push(arr[0][i] - arr[1][i])
    end
    MathWizard.abs_arr(actual_arr)
  else
    (arr[0] - arr[1]).abs
  end
end