0
我有我用Ruby編寫的代碼,但是當試圖在irb中測試我的文件時,我收到:NoMethodError: undefined method 'find_displacement' for SymbolTable:Class
。我在這裏做錯了什麼?Ruby:未定義的方法錯誤
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
......謝謝 – 2012-02-07 03:32:49
+1 - Doh,在鍵盤上睡着了。 ;-) – 2012-02-07 03:44:20