Class: SquareGraph
- Inherits:
-
Object
- Object
- SquareGraph
- Defined in:
- lib/square_graph.rb
Defined Under Namespace
Classes: Face
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
- #==(sg2) ⇒ Object
- #anytrue? ⇒ Boolean
- #clear ⇒ Object
- #each ⇒ Object
- #each_obj ⇒ Object
- #each_pos ⇒ Object
- #empty? ⇒ Boolean
- #falsey? ⇒ Boolean
- #fill(x, y, o = true) ⇒ Object
- #get(x, y) ⇒ Object
-
#initialize(*dimen) ⇒ SquareGraph
constructor
A new instance of SquareGraph.
- #need_truthy ⇒ Object
- #objects ⇒ Object
- #remove(*args) ⇒ Object
- #resize(length, width) ⇒ Object
- #truthy ⇒ Object
- #truthy?(&alt_cond) ⇒ Boolean
Constructor Details
#initialize(*dimen) ⇒ SquareGraph
Returns a new instance of SquareGraph.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/square_graph.rb', line 5 def initialize(*dimen) raise ArgumentError if ![0,2].include?(dimen.size) if dimen.size == 2 test_fixnum(dimen[0], dimen[1]) raise ArgumentError if test_range([1, dimen[0]], [1, dimen[1]]) @length, @width = dimen[0], dimen[1] @sized = true else @sized = false end @sg = Hash.new end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
3 4 5 |
# File 'lib/square_graph.rb', line 3 def length @length end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
3 4 5 |
# File 'lib/square_graph.rb', line 3 def width @width end |
Class Method Details
.create_truthy(objClass, &truthyMethod) ⇒ Object
129 130 131 132 |
# File 'lib/square_graph.rb', line 129 def self.create_truthy (objClass, &truthyMethod) return nil if objClass.method_defined? :truthy? objClass.send :define_method, :truthy?, truthyMethod end |
.create_truthy!(objClass, &truthyMethod) ⇒ Object
134 135 136 |
# File 'lib/square_graph.rb', line 134 def self.create_truthy! (objClass, &truthyMethod) objClass.send :define_method, :truthy?, truthyMethod end |
Instance Method Details
#==(sg2) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/square_graph.rb', line 151 def == sg2 sg2.each_pos do |p| return false if self.get(p[:x], p[:y]) != sg2.get(p[:x], p[:y]) end true end |
#anytrue? ⇒ Boolean
60 61 62 63 64 |
# File 'lib/square_graph.rb', line 60 def anytrue? !(@sg.any? do |f| @sg[f[0]] and @sg[f[0]].object end) end |
#clear ⇒ Object
52 53 54 |
# File 'lib/square_graph.rb', line 52 def clear @sg.clear end |
#each ⇒ Object
66 67 68 69 70 71 |
# File 'lib/square_graph.rb', line 66 def each return nil if @sg.empty? @sg.each_pair do |p, f| yield(f) end if block_given? end |
#each_obj ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/square_graph.rb', line 73 def each_obj return nil if @sg.empty? @sg.each_pair do |f, o| yield(o.object) end if block_given? @sg.each_pair if not block_given? end |
#each_pos ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/square_graph.rb', line 81 def each_pos return nil if @sg.empty? pos = Array.new (@length||@sg.keys.max(&comp('x'))[0]).downto(@sized?1:@sg.keys.min(&comp('x'))[0]).each do |l| (@width||@sg.keys.max(&comp('y'))[1]).downto(@sized?1:@sg.keys.min(&comp('y'))[1]).each do |w| p = Hash.new p[:x] = l p[:y] = w pos.push(p) end end pos.each do |p| yield(p) end if block_given? pos.each if not block_given? end |
#empty? ⇒ Boolean
56 57 58 |
# File 'lib/square_graph.rb', line 56 def empty? @sg.none? end |
#falsey? ⇒ Boolean
146 147 148 149 |
# File 'lib/square_graph.rb', line 146 def falsey? return false if @sg.size == 0 self.truthy.nil? end |
#fill(x, y, o = true) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/square_graph.rb', line 28 def fill(x, y, o = true) test_fixnum(x, y) if not o self.remove(x, y) if @sg[[x, y]] return nil end if @sized == true return nil if test_range([1, x], [1, y], [x, @length], [y, @length]) end return nil if @sg[[x,y]] @sg[[x,y]] = Face.new(x, y, o) end |
#get(x, y) ⇒ Object
41 42 43 44 |
# File 'lib/square_graph.rb', line 41 def get(x, y) return nil if !@sg[[x,y]] @sg[[x,y]].object end |
#need_truthy ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/square_graph.rb', line 120 def need_truthy result = Array.new @sg.each_pair do |p, f| result.push(f.object.class) if not f.object.class.method_defined? :truthy? end return nil if result.empty? result end |
#objects ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/square_graph.rb', line 98 def objects rt = Array.new @sg.values.each do |f| rt.push (f.object) end return nil if rt.size == 0 rt end |
#remove(*args) ⇒ Object
46 47 48 49 50 |
# File 'lib/square_graph.rb', line 46 def remove(*args) x, y = args[0], args[1] test_fixnum(x, y) @sg.delete([x, y]) end |
#resize(length, width) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/square_graph.rb', line 18 def resize(length, width) raise NoMethodError if @sized == false test_fixnum(length, width) return nil if test_range([1, length], [1, width]) if !@sg.empty? return nil if test_range([@sg.keys.max_by {|x| x[0]} [0], length], [@sg.keys.max_by {|y| y[1]} [1], width]) end @length, @width = length, width end |
#truthy ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/square_graph.rb', line 107 def truthy if @sized result = SquareGraph.new(@length, @width) else result = SquareGraph.new end @sg.each_pair do |p, f| result.fill(f.x, f.y, f.object) if f.truthy? end return nil if result.objects.nil? result end |
#truthy?(&alt_cond) ⇒ Boolean
138 139 140 141 142 143 144 |
# File 'lib/square_graph.rb', line 138 def truthy? (&alt_cond) return false if @sg.size == 0 @sg.each_pair do |p, f| return false if not f.truthy?(&alt_cond) end true end |