Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Sunday, March 13, 2016

Success with Ruby and BASIC

A while back, I started a project to learn Ruby. The project was to implement a version of BASIC, using that as a means to learn the Ruby language.

I am happy with my progress. I've learned a great deal about Ruby -- and learned more about programming. Ruby has a different "take" on programming than C++, Java, and C#, and those differences forced me to re-think a lot of ideas about programming. The design patterns for Ruby are different from the patterns of the "classical" object-oriented languages. I found that much of my code followed the classic patterns, and it worked poorly. Only after I changed my thinking about programming and my designs in Ruby did my code "work".

Rubocop helped too. It complained about many things, from spacing around operators (or lack thereof) to the number of lines and complexity of functions. At first I thought that some long functions would have to remain long -- in C++ or Java I would leave them as long functions. But with Rubocop's prodding, I redesigned them and made them smaller, many times breaking them into smaller functions.

i started using Rubocop after a significant amount of code had been written, and Rubocop reported somewhere north of 80 "violations". Over time, with thought and experimentation, I have reduced that number to one. The changes have, I believe, improved the code and made it more reliable and more readable.

My success has been due to the design of the Ruby language, the Rubocop tool, the Ruby documentation pages, and StackOverflow. These made it easy to develop in Ruby. Yet there were two other things: an open mind and time. I needed to change my thinking about programming, and accept the Ruby way of code. I also needed time. Time to think about the code, time to try things, and time to revise my initial code into something better.



Sunday, June 28, 2015

Building a BASIC interpreter in Ruby

I've been working on a side project: Build a BASIC interpreter in Ruby. The purpose is to help me learn the Ruby language, and to that end the project has worked well.

Learning a new programming language can be difficult. It's easy enough to write a simple "Hello, world!" program, but that simply confirms that the compiler (or interpreter) is installed correctly. What does one do next?

I picked the BASIC interpreter as a task with some complexity, but not too much. The BASIC language gives me a challenge but not one that is insurmountable. Also, I have an early text on programming in BASIC that provides example programs with their expected outputs.

As a bonus, programming a BASIC interpreter is a stroll down memory lane. BASIC was the first programming language that I learned. It is an old friend, one I have not seen in quite some time.

So as a project, the BASIC interpreter is challenging, supported, and fun.

I spell BASIC in all capitals because of the variant I am implementing. The BASIC language had a number of variants over time, from the early Dartmouth implementation in the 1960s to the DEC versions of the 1970s and 1980s, culminating in Visual Basic 6 from Microsoft. (Or perhaps VB.NET, but that seems less BASIC than any of the variants.)

My project is to implement an early version, one that is close to Dartmouth BASIC. It has a simplicity about it, yet it also has its intricacies. Dartmouth BASIC allows one to specify user-defined functions, but only on one line and only with a very limited set of names ('FNA' through 'FNZ'). It supports some elements of structured programming but still allows GOTO statements, and one can 'GOTO' from the inside of a loop to the outside, do some work, and 'GOTO' back into the loop. (One can also 'GOTO' out of a loop and not return into the loop.)

Early version or late, my experience has been a good one. I have been forced to learn the Ruby language. While I am not an expert, I am at least comfortable with the major constructs and classes of the language. And that was the point.

Tuesday, April 3, 2012

An infinite loop, and success with BASIC

Today I wrote a program. In BASIC. It had an infinite loop. By accident.

I think that the last time I wrote a BASIC program with an infinite loop (accidental or otherwise) was in the 1980s. (Probably the early 1980s.)

The program was a "Celcius to Fahrenheit" table generator. It was easy enough to write (I do remember BASIC) and it revealed some defects in the BASIC-1965 interpreter. One was in the parsing of arithmetic expressions and the other was in the computation of arithmetic expressions. The parsing was easily fixed, once identified. (I had the wrong logic for precedence of operators.) The calculation of an expression was a bit trickier; Ruby (the language that runs the BASIC interpreter) uses two types for numbers: Float and Integer. My math worked for the add, subtract, and multiple operations; I had to adjust the divide operation to return either a Float or an Integer.

After resolving the problems, my BASIC program works as expected. Woot! Today counts as the day that my first "real" program ran successfully.

Monday, March 12, 2012

Ruby vs. C#

I've been working with C# on the day job and Ruby on a side project. Shifting between the two has been enlightening.

C# is a statically typed language; Ruby is dynamically typed. I have come to rely on statically typed definitions, so I am a decent coder in C#.

I find that the coding experience in Ruby is quite different. I cannot rely on the IDE to tell me when I have assigned an improper value - in Ruby one can assign anything to any variable. It is easy to forget the type of variable and assign the wrong thing; I find out only at runtime, usually with an error of "undefined  method (methodname) for (classname)".

To get things right in Ruby, I must be more disciplined in my thoughts and in my programming. I'm not sure that this is a bad thing... I'm also not sure that this is a good thing. At the moment, I know only that it is a different way to program.

Friday, March 9, 2012

.NET and Ruby

This week I attended the local CMAP meeting, with its presentation on SQL in Azure. I learned a lot; SQL in Azure is different from SQL in the server room due to the "can move at any time" aspect of cloud computing. The differences are not particularly large, but can be significant (especially if you have built your system around one of the server room idiosyncrasies).

The CMAP meetings are consistently good for networking. They draw a sizable collection of people, and the attendees have varied projects and talent. I always have interesting conversations at them, and this month's meeting was no exception.

Later in the week I worked on my side project of BASIC-1965, an interpreter for BASIC written in Ruby. It's a fun project and a learning experience. Ruby has a different approach to programming, and my Java- and C#-trained brain is making mistakes as a learn "the Ruby way".