The languagesPython andRuby have the following in common:
They are free software (i.e., The source code can be easily accessed, and the interpreter that we will use for each language is written in the language itself);
They both support Object-Oriented Programming. (Python is actually Multi-Paradigm)
They are distributed with lightweight interactive consoles (IDLE forPython, irb (interactive Ruby) forRuby)
Therefore, it is worthwhile to explore these two languages to solve mathematical problems. In this book, we shall enumerate what they bring to the teaching of mathematics and algorithms. In particular, we will study how certain mathematical structures are managed by these two languages.
Note: Some features ofPython 3.2 will be used here (for example, the fact that division is exact by default and not integer division, the presence ofprint, the fact that thefraction object is available in Python 3.2...).
Two methods have been used to write the Python scripts below:
Run the code directly in the interactiveIDLE console, which is itself written inPython;
Create a script filetest.py, and run that from the console usingpython test.py orpython3.2 test.py, as the context requires.
ForRuby, version 1.9.1 will be used. Here too, two methods have been used:
Run the code in the interpreterirb (Interactive Ruby) which is itself written inRuby, so its source code is human-readable;
Create a script filetest.rb, and run it using the console commandruby test.rb.
TheFreeRide program is a lightweight, easy-to-use code editor that works well withRuby.FreeRide can execute theRuby program in the active window without having to leave the editor by simply clickingRun. One may preferNetBeans for its more extensive feature set and integration with Java, butNetBeans requires more system resources. Both programs are available for Windows, Mac OS X, and Linux, and both can handleRuby projects, which may include multiple (related) text files containingRuby programs. Anyway, for this book,geany has been used, mainly because it handles both Python and Ruby (amongst many others), and is rather lightweight.
Note: Even though this feature has not been used here, there are online interpreters for bothRuby andPython, so you can doPython orRuby programming in a web browser without having to install any additional software. The online interpreter forRuby can be accessed by following the link:[1]. To see an equivalent forPython, with some extensions for working with mathematical structures, see SAGE).