Class: Remedy::Size

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*new_dimensions) ⇒ Size

Returns a new instance of Size.



3
4
5
6
7
8
9
10
11
# File 'lib/remedy/size.rb', line 3

def initialize *new_dimensions
  new_dimensions.flatten!
  if new_dimensions.first.is_a? Range then
    new_dimensions.map! do |range|
      range.to_a.length
    end
  end
  @dimensions = new_dimensions
end

Instance Attribute Details

#dimensionsObject

Returns the value of attribute dimensions.



12
13
14
# File 'lib/remedy/size.rb', line 12

def dimensions
  @dimensions
end

Class Method Details

.zeroObject



14
15
16
# File 'lib/remedy/size.rb', line 14

def self.zero
  self.new([0,0])
end

Instance Method Details

#-(other_size) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/remedy/size.rb', line 18

def - other_size
  if other_size.respond_to? :length then
    self.class.new subtract(other_size)
  else
    self.class.new deduct(other_size)
  end
end

#/(value) ⇒ Object



26
27
28
29
30
# File 'lib/remedy/size.rb', line 26

def / value
  dimensions.map do |dimension|
    dimension / value
  end
end

#<<(value) ⇒ Object



32
33
34
# File 'lib/remedy/size.rb', line 32

def << value
  self.dimensions << value
end

#[](index) ⇒ Object



56
57
58
# File 'lib/remedy/size.rb', line 56

def [] index
  dimensions[index]
end

#colsObject Also known as: columns, width



50
51
52
# File 'lib/remedy/size.rb', line 50

def cols
  dimensions[1]
end

#fits_into?(size_to_fit_into) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/remedy/size.rb', line 37

def fits_into? size_to_fit_into
  other_size = Size(size_to_fit_into)
  length.times.each do |index|
    return false if self[index] > other_size[index]
  end
  true
end

#inspectObject



76
77
78
# File 'lib/remedy/size.rb', line 76

def inspect
  "#<#{self.class}:#{to_s}>"
end

#lengthObject



60
61
62
# File 'lib/remedy/size.rb', line 60

def length
  dimensions.length
end

#rowsObject Also known as: height



45
46
47
# File 'lib/remedy/size.rb', line 45

def rows
  dimensions[0]
end

#to_aObject



64
65
66
# File 'lib/remedy/size.rb', line 64

def to_a
  dimensions.dup
end

#to_aryObject



68
69
70
# File 'lib/remedy/size.rb', line 68

def to_ary
  dimensions
end

#to_sObject



72
73
74
# File 'lib/remedy/size.rb', line 72

def to_s
  "(#{dimensions.join('x')})"
end