Thursday, 24 July 2014

Using rlwrap with Ruby IRB

When you install Ruby on Raspberry Pi its interactive console, IRB, doesn't have console history. So if you enter a command incorrectly, you can't simply press the up arrow to get the old line back and change it. You have to type the whole command again.

A simple solution is to install rlwrap:

    sudo apt-get rlwrap

Then to run irb you simply do so via rlwrap:

    rlwrap irb


Ruby/GTK3 on Raspberry Pi

I've decided to give the GTK3 library a try for writing small GUI applications in Ruby on the Raspberry Pi. My two main reasons are that the Ruby GTK3 Gem was easy to install and, I believe, that GTK3 will benefit from the progressive move to Wayland in Raspian.

Installing the Ruby GTK3 Gem was simply a matter of a single command and a little patience. It took some time for all the native extensions to be built.

       gem install gtk3


Once the gem was installed, the Ruby Gnome 2 Hello World worked with the minor change of requiring gtk3 instead of gtk2.

If I have the time, I'm going to see if I can come up with a simple Domain Specific Language that would make it easier for children to write GUI programs in Ruby.

Compling JFFI on Raspberry Pi

I was trying to get JRuby working on my Raspberry Pi so that I could install Ruby Shoes 4. One problem that I came across was that JRuby won't run with the installed version of the Java Foreign Function Interface (JFFI). I found a solution in the JRuby Github Issues.

These are the commands to enter in the terminal:

cd ~
pi@raspberrypi:~$ git clone git://github.com/jnr/jffi.git jffi.git
cd jffi.git
ant jar
sudo cp libjffi-1.2.so /usr/lib
rm -r jffi.git



That got me past the JRuby issue with the JFFI, then I ran into a issue when JRuby tried to import java.lang.reflect.Modifier. It is apparent a bug in the Iced Tea Open JDK ARM builds.

I've decided to wait for a while and see if the issue get resolved before trying any other alternatives.

Monday, 14 July 2014

Installing Java, Ant and Maven on Raspberry Pi

In order to run Ruby Shoes 4 on my Raspberry Pi, I need to install JRuby. It hasn't been as straight forward as I'd like.The Raspian JRuby packages are very old and there is an issue that is stopping rbenv installing JRuby. Some of my problems could well be memory related as my Raspberry Pi is a model A.

I decided that I'd try to build JRuby from source. Building JRuby from source requires both Ant and Maven. When I went to install Maven with apt-get, the package manager also wanted to install so many other things including what seemed like three different Java runtimes.

I decided to install Ant and Maven myself. I installed Oracle's Java 1.7 Development Kit with apt-get:

    sudo apt-get install oracle-java7-jdk

I then used the instructions at the The Tao of Mac as a basis from which to directly install Ant and Maven.

The first thing to do was to set the JAVA_HOME environment variable which is required by Maven. I added the following line to my .bash_profile file in my home directory:

    JAVA_HOME="/usr/bin/java"

I downloaded the Ant 1.9.4 binary from Apache into my home directory, then deleted the manual folder using the File Manager. I then created a Java directory  in the /opt directory:

    sudo mkdir /opt/java

Next I moved Ant into that directory:

    sudo mv apache-ant-1.9.4 /opt/java

I then created a soft link to that directory to make it easier to install new versions of Ant in the future:

    cd /opt/java
    sudo ln -s apache-ant-1.9.4 ant

Next I added the ant/bin directory to the $PATH variable in my .bash_proifle:

    PATH="/home/pi/.rbenv/bin:$PATH:/opt/java/ant/bin"

The last thing with Ant was to check that it had been installed properly:

   pi@raspberrypi:/opt/java$ ant -version
   Apache Ant(TM) version 1.9.4 compiled on April 29 2014

The procedure for Maven was very similar, though this time I used wget to retrieve the Maven binary from the command line:

   cd ~
    wget http://www.apache.org/dist/maven/binaries/apache-maven-3.2.1-bin.tar.gz
    sudo tar -zxvf ~/apache-maven-3.2.1-bin.tar.gz
    sudo ln -s apache-maven-3.2.1 maven

Last thing to do was update my $PATH variable to include the Maven directory:

Of course, not installing Ant and Maven via the package manager means I will need to keep them up-to-date manually. I don't see that being a problem as I don't expect them to change that frequently or for me to make a lot of use of them.

Note: I have used my .bash_profile to store my $PATH and other environment variables. This means that I have to run a bash shell for them to come into effect. The standard Terminal on the Raspian desktop does not default to bash. The easiest way that I have found to open a terminal session on the desktop running bash is to use the "start" menu at the bottom lefthand corner of the screen and select Others-> bash. (I normally access the Raspberry Pi by using ssh from a MacBook. The terminal on the Mac loads bash by default.) 

Another thing to remember is that when changing your .bash_profile the changes don't take effect immediately. If you are using the desktop terminal, you need to close the terminal window and re-open it for the changes to take effect. If you are logged in to the shell, either directly or via ssh, you will need to logout and login again.

Wednesday, 9 July 2014

Raspberry Shoes

As far as I can tell, one of the motivations behind the development of the Raspberry Pi was the experience of actually typing code into a computer before you could play a game. The computers of those days such as the Acorn/BBC computers, the Sinclair range and others had languages with built-in access to the computers screen. There was no need to import and make calls to massive external libraries as is the case with most languages today.

There are a few exceptions  around, Rebol for instance. The stable version, Rebol 2, is not available on ARM processors. It's modern replacement, Rebol 3, which can run on ARM is still being developed, though progress is very slow. By the time it reaches version 1.0, the Red Programming Language will include easy to use access to the computer's screen.

Another alternative is Ruby/Shoes, a version of Ruby with built-in graphics. It seems to have taken a lot of inspiration from Rebol's approach. The first three versions of Shoes were written in C with a specific version of Ruby builtin. The latest version of Shoes, unsurprisingly Shoes 4, has been re-written from the ground up in Ruby. Actually it has been developed for the Java-based JRuby and uses the graphics features made available through Java.

I think I'll try to install Shoes 4 on my Raspberry Pi and see were it leads. It would be really good to have a simple way of programming graphic applications on the Raspberry Pi.

Dart Language (ECMA-408) and Unicode

I saw a Tweet about Google's Dart language specification being approved by ECMA (European Computer Manufacturers Association). It reminded me about Dart and I thought "It's a new language, I should put it through the UnicodeOutOfTheBoxTests".

I had second thoughts when I started to download Dart and noticed that it was over 100Mb. So I checked the new published ECMA-408 and found that Dart strings are UTF-16 and there appears to be no other Unicode support.

I came to the conclusion that it wasn't worth the time to write the Unicode tests in Dart as the results would be identical to JavaScript.

Tuesday, 8 July 2014

64 Bit Integer Arithmetic For Red/System

Red/System is the low-level dialect of the Red Programming Language. It is a compiled language, its datatypes and variables work like C, whereas its syntax is very similar to Red. The Red runtime is written in Red/System and the Red compiler outputs Red/System code (which is then compiled to native code).

The current version of Red/System has been written to meet those two needs and to do little more. Once Version 1.0 of Red is complete, the Red/System compiler will be re-written in Red (the current one is written in Rebol).

Using Red/System, it is very easy to write interfaces to external libraries written in C. There is a slight catch however, integers in Red/System are 32 bit integers and some libraries return 64 bit integers.

I've written a simple 64 bit integer arithmetic library for Red/System to make it easier to deal with such libraries. I expect that Red/System 2 will include built-in support for 64 bit integers so the library reflects its temporary nature.

The main functions are add, subtract, multiply, and divide. I used easy algorithms such as long multiplication and division.  There are a number of supporting functions and macros too.

You can find the library on Github.

Monday, 7 July 2014

Which Version Of Raspbian Is Installed?

This command displays which version of Raspbian is installed:

     cat /etc/*-release

It displays the following information:

    PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"
    NAME="Debian GNU/Linux"
    VERSION_ID="7"
    VERSION="7 (wheezy)"
    ID=debian
    ANSI_COLOR="1;31"
    HOME_URL="http://www.debian.org/"
    SUPPORT_URL="http://www.debian.org/support/"
    BUG_REPORT_URL="http://bugs.debian.org/"

Saturday, 5 July 2014

Installing Forth on Raspberry Pi

I had a very tiring day yesterday and wanted to read something a little relaxing in the evening, definitely nothing directly related to my work. So I scanned through my growing list of eBooks, many of them still unread, and came across my copy of Starting Forth by Leo Brodie. (You can read the book online on Forth Inc's website.)

I have been helping out with the development of Red Programming Language since its early days. Red is heavily influenced by Rebol which in turn has elements of Forth in it. Even after reading the first few pages of Starting Forth, I was able to better understand part of Red's roots.

For a long time now, my approach to programming has been to build things step at a time. The Forth philosophy exemplifies this approach with the exception of automated testing. (I need to investigate unit testing in Forth).

I believe that children tend to learn in small steps, they discover something new (perhaps with the help of a teacher) and then practice and practice. When they are comfortable with what they've just learnt, they move on to learn something else. It occurred to me that Forth could be a good language for children to learn and what better computer to learn Forth on than a Raspberry Pi?

There are lots of different Forths about. I chose the open-source GForth which is readily available for Raspbian. A single terminal command:

    sudo apt-get install gforth

A few minutes later, Forth was ready to go:

GForth on Raspberry Pi
Click to Enlarge

Wednesday, 2 July 2014

Installing Ruby on Raspberry Pi

One of the reasons that I bought my Raspberry Pi was for Hannah, our seven-year old daughter, to use it to learn about computers. She's already played with Scratch on my laptop. She's tried the Python games out on the Raspberry Pi. I guess there's even a possibility that she may get some exposure to Raspberry Pi at school. Perhaps I should start referring to it as "our" Raspberry Pi rather than  "my" Raspberry Pi.

I'm really keen for her to learn more about computers than using Microsoft Office and Google Drive. When the "Hello Ruby" children's book project was launched on Kickstarter, I quickly backed the project. If the project is on schedule the book should arrive during Hannah's summer holidays.

So I thought I'd install Ruby on our Raspberry Pi before the book arrives. I know a little Ruby and quite like it. More than that I've found it very useful on a number of occasions. I don't know Python at all so I'm planning to use Ruby on  our Raspberry Pi.

Installing Ruby wasn't difficult. I followed the clear instructions relating to Ruby and rbenv at Raw Syntax. There seems to be one slight omission in the instructions, you need to add rbenv to your PATH environment variable before trying to run rbenv to install Ruby.

After installing rbenv from Github, I created a .bash_profile file in my home directory with the following contents:

    PATH="/home/pi/.rbenv/bin:$PATH"
    eval "$(rbenv init -)"

I was then able to proceed to install the latest release of Ruby using this command:

    rbenv install 2.1.2

It takes a long time to compile Ruby on a Raspberry Pi so I left it running whilst I went for dinner. When I came back I was disappointed to find that the Ruby build had failed on the last stage - building the documentation. The error message was [rdoc] Error 137. I quickly googled the error and found a suggestion to install Ruby without its documentation. That was fine for me as when I have a question about Ruby I either look in The Pick Axe or check the online documents. This time I installed Ruby using:

    CONFIGURE_OPTS="—disable-install-doc" rbenv install 2.1.2

This time the install completed successfully and I now have Ruby installed on our Raspberry Pi


Ruby on Raspberry Pi
Click to Enlarge
Footnote: I haven't quite finished yet. Whilst I can run Ruby on our Raspberry Pi by using a terminal on my Mac, I'm having problems getting rbenv to work using LXTerminal on the Raspberry Pi. I can run Ruby by specifying the full path to the file but that defeats some of the purpose of installing rbenv. I'm pretty sure that it's something to do with LXTerminal and shells. I need to find out.

LXTerminal does not seem to run bash by default so the answer is either to select bash from the pop-up menus (Other -> Bash) or enter the following command in LXTerminal:
    
    lxterminal -e "bash -il"