Class: TreeHaver::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/tree_haver/point.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

Examples:

Method access

point = TreeHaver::Point.new(5, 10)
point.row    # => 5
point.column # => 10

Hash-like access

point[:row]    # => 5
point[:column] # => 10

Converting to hash

point.to_h # => {row: 5, column: 10}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, column) ⇒ Point

Create a new Point

Parameters:

  • row (Integer)

    the row (line) number, 0-indexed

  • column (Integer)

    the column number, 0-indexed



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

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

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



22
23
24
# File 'lib/tree_haver/point.rb', line 22

def column
  @column
end

#rowObject (readonly)

Returns the value of attribute row.



22
23
24
# File 'lib/tree_haver/point.rb', line 22

def row
  @row
end

Instance Method Details

#[](key) ⇒ Integer?

Hash-like access for compatibility

Parameters:

  • key (Symbol, String)

    :row or :column

Returns:

  • (Integer, nil)

    the value or nil if key not recognized



37
38
39
40
41
42
# File 'lib/tree_haver/point.rb', line 37

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

#inspectString

Inspect representation

Returns:

  • (String)


61
62
63
# File 'lib/tree_haver/point.rb', line 61

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

#to_hHash{Symbol => Integer}

Convert to a hash

Returns:

  • (Hash{Symbol => Integer})


47
48
49
# File 'lib/tree_haver/point.rb', line 47

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

#to_sString

String representation

Returns:

  • (String)


54
55
56
# File 'lib/tree_haver/point.rb', line 54

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