Class: YearArray::Yarray
- Inherits:
-
Object
- Object
- YearArray::Yarray
- Includes:
- TimeHelpers
- Defined in:
- lib/year_array/yarray.rb
Instance Attribute Summary collapse
-
#arr ⇒ Object
readonly
Returns the value of attribute arr.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #add(other) ⇒ Object
- #any?(&block) ⇒ Boolean
- #any_negative? ⇒ Boolean
- #any_positive? ⇒ Boolean
- #divide(other) ⇒ Object
-
#initialize(year, value: 0.0, arr: []) ⇒ Yarray
constructor
A new instance of Yarray.
- #multiply(other) ⇒ Object
- #raise_error_on_misalignment(other) ⇒ Object
- #size ⇒ Object
- #subtract(other) ⇒ Object
- #to_s ⇒ Object
- #year ⇒ Object
Methods included from TimeHelpers
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
#arr ⇒ Object (readonly)
Returns the value of attribute arr.
4 5 6 |
# File 'lib/year_array/yarray.rb', line 4 def arr @arr end |
#start_time ⇒ Object (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
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
84 85 86 |
# File 'lib/year_array/yarray.rb', line 84 def any?(&block) @arr.any?{|v| yield(v)} end |
#any_negative? ⇒ Boolean
92 93 94 |
# File 'lib/year_array/yarray.rb', line 92 def any_negative? any?{|e| e<0.0} end |
#any_positive? ⇒ 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
96 97 98 |
# File 'lib/year_array/yarray.rb', line 96 def raise_error_on_misalignment(other) raise MisalignmentError if !same_year?(other) end |
#size ⇒ Object
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_s ⇒ Object
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 |
#year ⇒ Object
18 19 20 |
# File 'lib/year_array/yarray.rb', line 18 def year @start_time.year end |