OO CSV Parser Lab
Introduction
The purpose of this lab is to gain experience using objects in Ruby. You will be taking a solution to the CSV parsing assignment and converting it to a form similar to what will be required for the JSON assignment. Unlike other assignments in this class, it is ok to work on the code for this lab in teams or groups.
Part 1 - Conversion
- Download the reference solution for the CSV parsing assignment from Canvas. Read through it to see how it works, and run it to verify that it passes the included unit tests.
- Create a new
CSVParser
class. The originalparse_CSV_file
method should become the class constructor, and theparse_line
method will become a helper instance method. - In Ruby, you do not need to explicitly declare instance variables; you
may simply use them. Instance variables are marked with an
@
symbol. Change the CSVParser class to use two instance variables:delim
andrecords
. Both of these should be initialized in the constructor. - Change
parse_line
so that it uses the@delim
instance variable and no longer takes the delimiter as a parameter. - Write a
get_records
method that retrieves the parsed CSV records. - Write a new
parse_CSV_file
method outside theCSVParser
class that implements the functionality required by the project spec, using an instance of theCSVParser
class. - Verify that all of the tests are still passing.
DFA Simulation
Download dfa.rb
. This file contains
two classes (State
and DFA
) for simulating
deterministic finite automata (DFAs -- state machines). Most of the code has
already been written, so read through it to understand how it works.
The State
object stores information for DFA states, including a
transition table stored as a Ruby Hash object. The DFA
object
stores an overall DFA, with a Ruby Hash of State
objects indexed by
ID.
Implement the accept?
method in the DFA
class.
This method should simulate the DFA working on a string of input characters. It
should return true if the DFA accepts the string, and false if it does not. When
you have completed this method, all of the included tests should pass.
HINT: The accept?
method should be very simple
(only about 4-5 lines of code)! Think about what it means to "simulate" a DFA.
You will need to simulate a state transition walk through the DFA, beginning at
the start state, following state transitions until all input is consumed, and
then checking to see if the ending state is final or not.