Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7190135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:26:58+00:00 2026-05-28T19:26:58+00:00

I am building a lexical analyzer in Ruby and am about to start gathering

  • 0

I am building a lexical analyzer in Ruby and am about to start gathering and storing symbols in the symbol table. My main question about the design of the symbol and as to whether it should be static table (meaning that all of the data will be held at the class level) or whether it should be on an instance to instance basis.

Option 1: Class level data structure

require 'SymbolTableEntry.rb'

class SymbolTable
  @sym_table = Array.new(500)
  def initialize()
  end

  def SymbolTable.add(element, index)
    @sym_table[index] = element if element.is_a? SymbolTableEntry
  end

  def SymbolTable.to_s
    pp @sym_table
  end
end

With this scheme, the SymbolTable class has a sort of ‘static’ functionality, meaning that I don’t actually create an instance of a SymbolTable, the only object that exists is the class level one.

(Assume that SymbolTableEntry is a valid object even though I don’t define it here)

Ex:

irb(main):002:0> require 'SymbolTable.rb'
=> true

irb(main):003:0> ste = SymbolTableEntry.new
=> #<SymbolTableEntry:0x7ef36884>

irb(main):004:0> SymbolTable.add(ste, 10)
=> #<SymbolTableEntry:0x7ef36884>

irb(main):005:0> SymbolTable.to_s
[nil,
 nil,
 nil,
 nil,
 nil,
 nil,
 nil,
 nil,
 nil,
 nil,
 #<SymbolTableEntry:0x7ef36884>]
=> nil

Option 2: Instance level data structure

require 'rubygems'
require 'backports'
require 'SymbolTableEntry.rb'

class SymbolTable
  def initialize()
    @sym_table = Array.new(10)
  end

  def add(element, index)
    @sym_table[index] = element if element.is_a? SymbolTableEntry
  end

  def to_s
    pp @sym_table
  end
end

With this scheme I would actually need to instantiate an instance of the SymbolTable class in order to add values to the symbol table.

irb(main):001:0> require 'SymbolTable.rb'
=> true

irb(main):002:0> st = SymbolTable.new
=> #<SymbolTable:0x7eeb6c9c @sym_table=[nil, nil, nil, nil, nil, 
                                        nil, nil, nil, nil, nil]>

irb(main):003:0> ste=SymbolTableEntry.new
=> #<SymbolTableEntry:0x7eeb4d5c>

irb(main):004:0> st.add(ste,10)
=> #<SymbolTableEntry:0x7eeb4d5c>

irb(main):007:0> st.to_s
[nil,
 nil,
 nil,
 nil,
 nil,
 nil,
 nil,
 nil,
 nil,
 nil,
 #<SymbolTableEntry:0x7eeb4d5c>]
=> nil

I would love to hear any and all input on which design you would use or prefer to use, as well any comments on the ruby code in general.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:26:59+00:00Added an answer on May 28, 2026 at 7:26 pm

    Prefer instance variables over class variables, if for no other reason than that classes using class variables are a bit more of a pain to unit test.

    You can use instance variables and still have one symbol table to rule them all. One way is by assigning your symbol table to a global variable:

    $symbol_table = SymbolTable.new
    

    In some languages, classes that use global variables are difficult to test. In Ruby, they’re not so bad, since duck typing lets you assign mock objects to global variables before executing the object under test.

    Or, you can use the singleton pattern. Ruby comes with a library to make this easy:

    require 'singleton'
    class SymbolTable
      include Singleton
      ...
    end
    

    To retrieve the one-and-only instance of SymbolTable, created it if needed:

    SymbolTable.instance
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a lexical analysis engine in c#. For the most part it is
Building on what has been written in SO question Best Singleton Implementation In Java
I am presently learning Lexical Analysis in Compiler Design. In order to learn how
building on my last question jQuery getting values from multiple selects together I have
Building on this question , is there a simple solution for having a multi-key
building upon the $.fn.serializeObject() function from this question , i'd like to be able
Building off this question, How to implement an achievement system in RoR would I
Building on this question , now I have another problem. Given this, shipments =
Building on How Do You Express Binary Literals in Python , I was thinking
Building a client-side swing application what should be notified on a bus (application-wide message

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.