Class: YearArray::Yarray

Inherits:
Object
  • Object
show all
Includes:
TimeHelpers
Defined in:
lib/year_array/yarray.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TimeHelpers

included

Constructor Details

#initialize(year, value: 0.0, arr: []) ⇒ Yarray

Returns a new instance of Yarray.



6
7
8
9
10
11
12
# File 'lib/year_array/yarray.rb', line 6

def initialize(year, value: 0.0, arr: [])
  @start_time = Time.new(year,1,1)
  nohiy = Yarray.hours_in_year(year)
  arr = [] if arr.nil?
  arr = arr.first(nohiy)
  @arr = arr + Arr.new(nohiy-arr.size, value)
end

Instance Attribute Details

#arrObject (readonly)

Returns the value of attribute arr.



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

def arr
  @arr
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

Class Method Details

.max(ya1, ya2) ⇒ Object



78
79
80
81
82
# File 'lib/year_array/yarray.rb', line 78

def self.max(ya1, ya2)
  ya1.raise_error_on_misalignment ya2
  a = Arr.new(ya1.size){ |i| ya1.arr[i]>ya2.arr[i] ? ya1.arr[i] : ya2.arr[i] }
  Yarray.new(ya1.year, arr: a)
end

.min(ya1, ya2) ⇒ Object



71
72
73
74
75
76
# File 'lib/year_array/yarray.rb', line 71

def self.min(ya1, ya2)
  ya1.raise_error_on_misalignment ya2
  a = Arr.new(ya1.size){ |i| ya1.arr[i]<ya2.arr[i] ? ya1.arr[i] : ya2.arr[i] }

  Yarray.new(ya1.year, arr: a)
end

Instance Method Details

#*(other) ⇒ Object



59
60
61
62
63
# File 'lib/year_array/yarray.rb', line 59

def *(other)
  raise_error_on_misalignment other
  a = Arr.new(size){ |i| arr[i]*other.arr[i] }
  Yarray.new(year, arr: a)
end

#+(other) ⇒ Object



47
48
49
50
51
# File 'lib/year_array/yarray.rb', line 47

def +(other)
  raise_error_on_misalignment other
  a = Arr.new(size){ |i| arr[i]+other.arr[i] }
  Yarray.new(year, arr: a)
end

#-(other) ⇒ Object



53
54
55
56
57
# File 'lib/year_array/yarray.rb', line 53

def -(other)
  raise_error_on_misalignment other
  a = Arr.new(size){ |i| arr[i]-other.arr[i] }
  Yarray.new(year, arr: a)
end

#/(other) ⇒ Object



65
66
67
68
69
# File 'lib/year_array/yarray.rb', line 65

def /(other)
  raise_error_on_misalignment other
  a = Arr.new(size){ |i| arr[i]/other.arr[i] }
  Yarray.new(year, arr: a)
end

#add(other) ⇒ Object



22
23
24
25
26
# File 'lib/year_array/yarray.rb', line 22

def add(other)
  raise_error_on_misalignment other
  @arr.each_with_index{|v, i| @arr[i]=v+other.arr[i]}
  self
end

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/year_array/yarray.rb', line 84

def any?(&block)
  @arr.any?{|v| yield(v)}
end

#any_negative?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/year_array/yarray.rb', line 92

def any_negative?
  any?{|e| e<0.0}
end

#any_positive?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/year_array/yarray.rb', line 88

def any_positive?
  any?{|e| e>0.0}
end

#divide(other) ⇒ Object



40
41
42
43
44
# File 'lib/year_array/yarray.rb', line 40

def divide(other)
  raise_error_on_misalignment other
  @arr.each_with_index{|v, i| @arr[i]=v/other.arr[i]}
  self
end

#multiply(other) ⇒ Object



34
35
36
37
38
# File 'lib/year_array/yarray.rb', line 34

def multiply(other)
  raise_error_on_misalignment other
  @arr.each_with_index{|v, i| @arr[i]=v*other.arr[i]}
  self
end

#raise_error_on_misalignment(other) ⇒ Object

Raises:



96
97
98
# File 'lib/year_array/yarray.rb', line 96

def raise_error_on_misalignment(other)
  raise MisalignmentError if !same_year?(other)
end

#sizeObject



14
15
16
# File 'lib/year_array/yarray.rb', line 14

def size
  @arr.size
end

#subtract(other) ⇒ Object



28
29
30
31
32
# File 'lib/year_array/yarray.rb', line 28

def subtract(other)
  raise_error_on_misalignment other
  @arr.each_with_index{|v, i| @arr[i]=v-other.arr[i]}
  self
end

#to_sObject



100
101
102
# File 'lib/year_array/yarray.rb', line 100

def to_s
  "start_time: #{start_time}, arr: [#{arr[0..6].join(', ')}, ..., #{arr.last}]"
end

#yearObject



18
19
20
# File 'lib/year_array/yarray.rb', line 18

def year
  @start_time.year
end