Class: TreeHaver::Point

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

Overview

Point class that works as both a Hash and an object with row/column accessors

This provides compatibility with code expecting either:

  • Hash access: point, point

  • Method access: point.row, point.column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, column) ⇒ Point

Returns a new instance of Point.



12
13
14
15
# File 'lib/tree_haver/node.rb', line 12

def initialize(row, column)
  @row = row
  @column = column
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



10
11
12
# File 'lib/tree_haver/node.rb', line 10

def column
  @column
end

#rowObject (readonly)

Returns the value of attribute row.



10
11
12
# File 'lib/tree_haver/node.rb', line 10

def row
  @row
end

Instance Method Details

#[](key) ⇒ Object

Hash-like access for compatibility



18
19
20
21
22
23
# File 'lib/tree_haver/node.rb', line 18

def [](key)
  case key
  when :row, "row" then @row
  when :column, "column" then @column
  end
end

#inspectObject



33
34
35
# File 'lib/tree_haver/node.rb', line 33

def inspect
  "#<TreeHaver::Point row=#{@row} column=#{@column}>"
end

#to_hObject



25
26
27
# File 'lib/tree_haver/node.rb', line 25

def to_h
  {row: @row, column: @column}
end

#to_sObject



29
30
31
# File 'lib/tree_haver/node.rb', line 29

def to_s
  "(#{@row}, #{@column})"
end