Skip to end of metadata
Go to start of metadata

The goal for this project was to create a Linked List in Ruby.  Ruby is a rather polymorphic friendly language, much like Python, because functions do not require a type related to a parameter.  Therefore, many can be used for more than one type of parameter.  I made use of this in my implementation of a doubly linked list. 

Since Ruby does not have structs like C does, I used classes to implement the node and Linked List object:

 

Notice that unlike in JavaScript, there is a need to make get and set functions to access the fields of the classes, which makes this a more difficult implementation, although the ability to use more than one type in a function makes up for it.  

Many of the functions in this implementation are polymorphic: the functions that add data in a node to the linked list are polymorphic because any type can be used as the data.  The printWell function can be used to print both ints and chars, but the squareInt function can only be used on integers or floats because they are they only types where the multiplication of two of them is defined.  Similarly, addHi can only be used on strings because strings can only be concatenated with strings.  The comp function is polymorphic because it can be used on any types that can be compared with == (ints, floats, char). 

There are benefits to this implementation: less thought is needed about generic data types because Ruby is a less typed language than C.  Multiple data types can be added to the list and then the same functions can be used to modify it (see extensions below).  But, the class in Ruby requires explicit get and set functions, which seem rather unnecessary and wasteful. 

This is the output of the program:

Extensions:

  1.  The first extension I completed was to add a delete function to my LinkedList implementation. This allows the user to delete a node at any index in the list.  This challenged me to improve my looping through the list so I did it in a thoughtful and deliberate way (which actually made me go back and change some other functions).  The output of this is shown above. 
  2. The second extension that I completed was to figure out how to include a Ruby program in another program to be able to use its functions.  This made my code much more organized because my test functions can be separate from the main Linked List class and functions.  I also figured out how to get a Ruby main function to be called only when it is the file being called on the command line, not if it is included in another file.

  3. The third extension that I completed was to create another LinkedList to show that in Ruby it is possible to make and modify Linked Lists with multiple data types in the Nodes.  C would not be able to do this because although different functions can be used to modify different data types, it would be very hard to loop through a linked list and use a different function on different nodes based on the types of their node's item.  This is the output of making a list with an int, string, and float. 

 

 

 

Labels
  • No labels