Using Ruby from APLX

Using APLX it's fairly straightforward to make use of classes written in the Ruby programming language. You can create Ruby objects - including vectors of objects - and manipulate them from within APLX. The interpreter will handle conversion between APL and Ruby representations of the data, allowing fairly natural APL expressions.

In this example we create a Ruby DateTime object and examine it using APLX:

      ⍝ We'll be using the 'DateTime' class which is defined in the 'Date' module
      'ruby' ⎕SETUP 'require' 'Date'

      ⍝ Create a new date object - Christmas day 2008
      dt←'ruby' ⎕new 'DateTime' 2008 12 25

      ⍝ The APLX variable 'dt' is an object reference
      dt 
[ruby:DateTime]

      ⍝ We can display the date
      dt.to_s      
2008-12-25T00:00:00+00:00

      ⍝ What day of the week did Christmas 2008 fall on?
      dt.wday
4

      ⍝ Now let's try adding a day.
      ⍝ We need to call the Ruby DateTime method '+'. 
      ⍝ Since '+' has a different meaning to APL, we use the '$' character to escape it
      ⍝ What we get back is a new Ruby DateTime object. The original is immutable
      dt.$+ 1
[ruby:DateTime]
      (dt.$+ 1).to_s
2008-12-26T00:00:00+00:00

      ⍝ Because APL is an array language we can do this sort of thing:
      dt.$+ ¨10 20 30 40
[ruby:DateTime] [ruby:DateTime] [ruby:DateTime] [ruby:DateTime]
      (dt.$+ ¨10 20 30 40).wday
0 3 6 2
      ⎕W[⎕IO+(dt.$+ ¨10 20 30 40).wday;]
SUNDAY
WEDNESDAY
SATURDAY
TUESDAY

Here's another APLX example of using the Ruby Hash class, which maintains key/value pairs:

      ⍝ Create an object with class Hash 
      h←'ruby' ⎕NEW 'Hash'

      ⍝ It's initially empty, so add some values
      h.length
0
      h.store 'France' 'Paris'
Paris
      h.store 'UK' 'London'
London
      h.store 'Italy' 'Rome'
Rome
      h.store 'Germany' 'Berlin'
Berlin
      h.length
4

      ⍝ List the current members
      h.to_a 
  UK London   France Paris   Italy Rome   Germany Berlin
      h.sort            ⍝ Sort by key values, return array
  France Paris   Germany Berlin   Italy Rome   UK London
      
      ⍝ Now try some lookups
      ⍝ (The Ruby method is called 'key?' but we must escape the '?')
      h.key$? 'France'  ⍝ Does key 'France' exist? 
1
      h.key$? 'USA'     ⍝ Does key 'USA' exist? 
0
      h.fetch 'France'
Paris
      h.fetch 'USA'
#<IndexError: key not found>
DOMAIN ERROR
      h.fetch 'USA'
      ^

And here's another example in which we use the Ruby Socket class to do a DNS lookup - i.e. convert a Domain Name into an IP address. Notice that in this example we don't create a Socket object; instead the example uses ⎕GETCLASS to get a class reference and we then call the class method 'getaddrinfo'

      ⍝ We'll be using the Socket class:
      'ruby' ⎕SETUP 'require' 'socket'
 
      ⍝ Get a class reference 
      socket←'ruby' ⎕GETCLASS 'Socket'

      ⍝ Try a DNS lookup. The result returned is a vector...
      socket.getaddrinfo 'news.bbc.co.uk' 0
  AF_INET 0 newslb305.telhc.bbc.co.uk 212.58.226.73 2 2 17

      ⍝ ...which is more obvious if we show it like this:
      ⎕display socket.getaddrinfo 'news.bbc.co.uk' 0
┌→───────────────────────────────────────────────────────────────────┐
│ ┌→───────────────────────────────────────────────────────────────┐ │
│ │ ┌→──────┐   ┌→────────────────────────┐ ┌→────────────┐        │ │
│ │ │AF_INET│ 0 │newslb305.telhc.bbc.co.uk│ │212.58.226.73│ 2 2 17 │ │
│ │ └───────┘   └─────────────────────────┘ └─────────────┘        │ │
│ └∊───────────────────────────────────────────────────────────────┘ │
└∊───────────────────────────────────────────────────────────────────┘


CategoryAplx CategoryRuby

APLXExamplesRuby (last edited 2009-04-09 09:08:17 by SimonMarsden)