Hunter McMillen
Hunter McMillen

Reputation: 61515

Ruby: undefined method error

I have this code that I have written in Ruby, but when trying to test my file in irb I receive a: NoMethodError: undefined method 'find_displacement' for SymbolTable:Class. What am I doing wrong here?

class SymbolTable
  include Singleton

  @@MAX_SYMBOLS  = 500
  @@DISPLACEMENT = SymbolTable.find_displacement()   ##error here
  @@TABLE_SIZE   = @@MAX_SYMBOLS + (@@MAX_SYMBOLS * 0.1) + @@DISPLACEMENT

  def initialize()
    "Constructs a single instance of a SymbolTable to be used by the compiler"
      @sym_table = Array.new(@@TABLE_SIZE)
  end

  def add(element, index)
    "Inserts an element (identifier) into the SymbolTable"
    @sym_table[index] = element if element.is_a? SymbolTableEntry
  end

  def SymbolTable.find_displacement()
    n = 1
    k = @@MAX_SYMBOLS
    while not (k + n).odd? do
      n += 2
    end

    return k + n
  end

  def to_s
    "Prints a list of all elements currently in the SymbolTable"
    pp @sym_table
  end
end

Upvotes: 0

Views: 4079

Answers (1)

Marc-André Lafortune
Marc-André Lafortune

Reputation: 79552

You must define your method before calling it.

At the time your class variable is being set, no singleton methods have yet been defined.

Upvotes: 4

Related Questions