1

I have the following code from a Java class:

enum TokenType {
  CHR("[a-z]"),
  INTEGER("[0-9]"),
  OP_EQUALS("\\="),
  OP_PLUS  ("\\+"),
  OP_MINUS("\\-"),
  OP_MULTIPLY("\\*"),
  OP_DIVIDE("\\/"),
  OP_LESS("\\<"),
  LOGIC_AND("and"),
  LOGIC_NOT("not"),
  LOGIC_TRUE("true"),
  LOGIC_FALSE("false"),
  PUNCT_LEFTPAREN("\\("),
  PUNCT_RIGHTPAREN("\\)"),
  PUNCT_SEMIC("\\;"),
  EOF("\\#"),
  ;

  private TokenType(String ch) {
    this.tokenClass = ch;
  }
  String tokenClass;

  public static TokenType parse(String in) {
    for (TokenType type : TokenType.values()) {
      if (in.matches(type.tokenClass)) {
        return type;
      }
    }
    return null;
  }

}

As a Ruby newbie, I am trying to figure out to implement this in Ruby. The more simple the better. I tried the following based on some posts here, but can't seem to get it finished. I want the class to have a parse() method which matches an input string with all the enum options and return a null if none of the patterns match. Here's what I've tried so far:

 class TokenType
  attr_accessor :tokenClass
  def initialize(str)
     @tokenClass = str
  end

    CHR = new("[a-z]")
    INTEGER = new("[0-9]"),
    OP_EQUALS = new("\\="),
    OP_PLUS = new("\\+"),
    OP_MINUS= new("\\-"),
    OP_MULTIPLY= new("\\*"),
    OP_DIVIDE = new("\\/"),
    OP_LESS = new("\\<"),
    LOGIC_AND = new("and"),
    LOGIC_NOT = new("not"),
    LOGIC_TRUE = new("true"),
    LOGIC_FALSE = new("false"),
    PUNCT_LEFTPAREN = new("\\("),
    PUNCT_RIGHTPAREN = new("\\)"),
    PUNCT_SEMIC = new("\\;"),
    EOF= new("\\#"),

    class << self
      private :new
    end   

    def TokenType.parse(str_in)

    end
end

Any thoughts or ideas? Thanks.

runit
  • 61
  • 2
  • 7
  • You might start by reviewing this previous post: http://stackoverflow.com/questions/75759/enums-in-ruby – fmendez Apr 02 '13 at 00:01
  • Thanks for that reference, but I had already looked at that thread and still was only able to get as far my Ruby code above. – runit Apr 02 '13 at 01:16

1 Answers1

0

You are trying to write java code with ruby syntax. I would do the following:

def parse str
  case str
  when 'a'..'z' then CHR
  when 0..9 then INTEGER
  …
  end
end

You may decide to define constants within your class or do whatever you want, but matching is done for you by built-in facilities of ruby case clause.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Thanks very much. That seems straight forward enough. – runit Apr 03 '13 at 04:22
  • You are welcome. Actually, the most brainbraking thing for me during I was switching from java to ruby was to stop thinking in OOP paradigm in cases, where the OOP is more to a burden than to wings. – Aleksei Matiushkin Apr 03 '13 at 04:28
  • Actually, it seems that what the OP is asking about is a means of determining whether a string is in a set. If that's the case, the Set class will do it beautifully. If the intent is to map a string to a symbol, then Hash is what is needed. Perhaps you need to go from a symbol to a string as well, in which case two hashes might be useful. The core can be improved by factoring out any logic so the base is reusable with different strings and symbols. – user1164178 Feb 14 '15 at 06:59