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

Sunday, 25 February 2018

Installing Watir and Python Selenium on macOS

I have been using Watir on and off for quite a long time, seeing it develop from Watir through Watir-Webdriver and back to Watir. I needed to install its latest incarnation and hit a few problems. The problems were caused more by my impatience in not reading through the notes on the Watir website than anything else.

At the same time, I decided to install the Python Selenium package to try it out. It also didn't work out of the box. Once again, the problem was that I hadn't been sufficiently thorough in reading the instructions.

The main problem with both was the need to install the Firefox and Chrome web drivers and to configure Safari's. After a little trial and error and reading through  the documents, the simplest way came to the surface.

I installed the Firefox and Chrome web drivers using Homebrew: 
    brew install geckodriver
    brew install chromedriver

My Safari installation was already configured to show the Develop menu and I have Allow Remote Automation menu item selected. From the Watir Safari Driver page, I found that I need to run safaridriver once from the command line to give it the proper authorities. I needed to run it from an administrator account:
     sudo /usr/bin/safaridriver

After that both Watir and Python Selenium both will happily load Safari, Firefox and Chrome when run from a macOS user account.

Tuesday, 20 September 2016

Lua Patterns, Red Parse, and Regex

In my Learning Lua project, I needed to extract a four character code from a string. Many languages use a builtin Regex implementation to provide the capability to match and extract patterns from strings. I am also familiar with Red, which like Rebol before it, provides the Parse Dialect (a Domain Specific Language (DSL)) which not only has excellent string matching and extraction features but makes it easy to write further DSLs. 

I found that Lua has another approach. It has a built-in lightweight pattern language, known as PatternsFrom what I understand, it was designed to be simpler than Regex both in its "grammar" and its capability. (Lua does have Regex modules available. It also has the very interesting LPeg pattern-matching library, based on Parsing Expression Grammars (PEGs).)

I thought it would be interesting to compare the three approaches, patterns, parse and regex. Here are three versions of a short function to extract the first four consecutive alphabetic characters from a string. The functions return an empty string if there is no match.

First Regex, courtesy of Ruby:

    def parse_line line
      ticker = line.match(/[A-Za-z]{4,4}/)
      if ticker then
        ticker[0]
      else
        ''
      end
    end

Note: 
    I'm no Regex expert, there may well  be a better way, and probably a more Rubyish way too.

Second Parse, courtesy of Red

parse-line: function [line [string!] /local ticker][
alpha: charset [#"A"-#"Z" #"a"-#"z"] parse line [to copy ticker 4 alpha] any [ticker ""] ]

Notes: 
    1. The charset allows you to efficiently define set of characters to be matched.
    2. The parse function apply the rules provided between the []s to the input.
    3. A parse rule can have any number of sub-rules.
    4. Red Parse details.

Finally Patterns, courtesy of Lua

    function stockfetch.parseLine (line)
      local match = string.match(line, '(%a%a%a%a)')
      if match then 
        return match
      else
        return ''
      end
    end

There doesn't seem much to chose between the approaches for such a simple task. The Lua pattern is very readable but you do have to know (or lookup) that %a matches an alphabetic character. The Regex is self-explanatory once you understand Regex syntax. The Red parse rule may seem a little more complicated because it also specifies to where the data are extracted (copy ticker) and has to ensure that the rules cover the whole supplied string (to end).


It will be interesting to see how the three approaches compare with more challenging requirements. Perhaps, I'll be able to add LPeg into the equation too.

Note: I am a strong supporter of and contributor to Red and am highly biased towards its Parse DSL. 

Sunday, 3 May 2015

Installing gtk3 Ruby Gem on OS X Yosemite

As I found installing the gtk3 Ruby Gem on OS X Yosemite is a little fiddly, I thought I'd make a note of how to do it for next time so that I don't need to spend time searching the internet to found out how.

First, you need to install X-windows. That's pretty straightforward simply click on the X.11 icon in the Others collection in LaunchPad and follow the instructions.

Second, you need to install gtk+3 itself. Using HomeBrew that's straightforward:

    brew install gtk+3

Then the tricky bit. You need to help gem find the correct libffi library. I found  out how to do it thanks to the Ruby-Gnome team:

   sudo PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig gem install gtk3

Wednesday, 4 March 2015

Trying to Like Python

Having fun with projects on the Raspberry Pi would be so easy if I could overcome my dislike, in all probability based on my misperceptions, of Python. When it comes to Raspberry Pi, Python sits at the top table whereas Ruby wouldn't even get the crumbs of the floor if it wasn't for Sonic Pi.

I still have a belief, may be an irrational one, that it is easier for young kids to learn Ruby than Python. However, I know for a fact that it is a infinitely easier for them to use Python in Raspberry Pi projects.

Update: I still am finding Python difficult to like.

Monday, 29 December 2014

Raspberry Shoes Update

I haven't written anything about Raspberry Shoes in a while. The reason is that Raspberry Shoes still feels a long, long way away.

The route that was most attractive for me was Shoes 4 which is written for JRuby and the SWT cross-platform GUI toolkit. Last time I wrote I was quite optimistic, I had JRuby working on the Raspberry Pi. Shortly before then, the Shoes 4 team announced a pre-release Gem that would have made installing Shoes quite straightforward on Raspberry Pi. However, after installing the Gem I found that the Ruby SWT Gem upon which Shoes 4 relies only installs Intel versions of SWT. It seems there isn't an official ARM version of SWT. So, for the foreseeable future, it is unlikely that Shoes 4 will run on Raspberry Pi.

As an alternative, I installed the Green Shoes Gem. Green Shoes is a version of Shoes that uses the GTK2 GUI toolkit. It didn't work and so far I haven't spent anytime trying to figure out why. That's because Green Shoes is understandably no longer maintained in the light of Shoes 4.

One option that came to mind is that I could build Raspberry Shoes by forking Green Shoes and convert it to use GTK+3 (which, in the long term, I presume will be better supported on Raspberry Pi than GTK2). However, I very much doubt that I have the necessary skill to get it done in the time I have available. (I'm confident that I could do it if I put enough time into it.)

I even had a "if you can't beat them, join them" moment and thought that perhaps taking the easy choice, Python, would be better than trying to use Ruby. Then I saw a couple of Raspberry Pi Python code snippets. They were enough to convince me that Ruby will be much easier for a child to learn.

At the moment, I am undecided on what to do. Another language which can be even easier for beginners and in which I am more fluent than Ruby will be coming to Raspberry Pi soon. The language is Red. It is very highly influenced by Rebol. It is currently at the "alpha" stage and is expected to reach "beta" stage sometime next year. 

In the meantime, I'll try to spend a little time with Ruby and GTK+3 to see if I can come up with something for our seven year-old to follow on from Hello Ruby when it eventually arrives.

Friday, 5 September 2014

Installing JRuby on Raspberry Pi

I am persisting with my project to get Ruby Shoes 4 up and running on my Raspberry Pi. I really believe that Shoes will be a great medium for kids to learn programming. I've now completed the next step, getting JRuby up and running on Raspberry Pi.

I'm using rbenv to manage my Ruby installations on my Raspberry Pi as I explained in Installing Ruby on Raspberry Pi.

Of course, you need Java to install and run JRuby. I've found that there is a bug with the OpenJDK version of Java that stops JRuby working. So I installed the Oracle version:

sudo install oracle-java7-jdk

After installing Oracle's Java, like me, you'll probably end up with multiple versions of Java on your Raspberry Pi. Thanks to Bill Connelly for dropping a big hint on the Raspberry Pi forums, I was able to easily set Oracle's Java as the default by running the following commands and selecting the correct option:

sudo update-alternatives —config java
sudo update-alternatives —config javac

After that I simply had to use rbenv to install version 1.7.13 of JRuby:

rbenv install jruby-1.7.13

It takes quite a while and keeps the Raspberry Pi fully occupied during the installation but you can leave it running and come back later.

I installed JRuby 1.17.13 instead of the newer JRuby 1.7.14 as there was a warning on Shoes 4 GitHub that there is an issue with the newer version.

As I have Ruby 2.1.2 as the default Ruby on my Raspberry Pi, I set up a new directory, shoes, in which I set JRuby as the default. These are the commands that I used:

cd ~/raspberryruby
mkdir shoes
cd shoes
rbenv local jruby-1.17.13

Finally I launched JRuby:

~/raspberryruby/shoes$ ruby -v
jruby 1.7.13 (1.9.3p392) 2014-06-24 43f133c on Java HotSpot(TM) Client VM 1.7.0_40-b43 [linux-arm]

When you run JRuby, it takes a long time to load. I think this is not just because of the time to load the Java Virtual Machine in which JRuby runs but also due to a small bug that results in JRuby displaying the following warning:

pi@raspberrypi:~/raspberryruby/shoes$ irb
Java HotSpot(TM) Client VM warning: You have loaded library /home/pi/.rbenv/versions/jruby-1.7.13/lib/jni/x86_64 Linux/libjffi-1.2.so which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.
irb(main):001:0> 

The warning doesn't have any adverse effect on JRuby, everything works as expected. This bug has already been reported to the JRuby team and hopefully will get fixed soon.

Now I can start on getting Shoes to run on my Raspberry Pi. 

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.

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.

Wednesday, 2 July 2014

Installing Ruby on Raspberry Pi

One of the reasons that I bought my Raspberry Pi was for our seven-year old 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"