RubyMotion Android Beta Tip

I have been trying out the new Android RubyMotion beta and have been trying out the code samples.

One thing I was frustrated about was having to keep putting the NDK and SDK path in the Rakefile. Here is a quick tip to make it easier.

Create a file somewhere like ~/motion/common.rb

@app.sdk_path = '/Android/sdk'
@app.ndk_path = '/Android/ndk'
@app.api_version = "19"
# other common configs

Inside the Rakefile for each app:

require 'motion/project/template/android'

Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings. = 'Paint'
@app = app
require '~/motion/common.rb'

Thanks to JP, you can also add this to your ~/.bash_profile or ~/.zshrc

export RUBYMOTION_ANDROID_SDK=~/android-rubymotion/sdk
export RUBYMOTION_ANDROID_NDK=~/android-rubymotion/ndk

If you have a better way to do it, post in the comments below.

It was interesting to find out how they confirmed that MH370 crashed over the indian ocean.

The planes navigation system was communicating to a satellite even though the main transponder was turned off and it communicated every hour. The Satcom “which collects information such as location, altitude, heading and speed, and sends it through Inmarsat’s satellites into their network”. The signal didnt contain any gps or location data so the analysts used techniques never used before, using the doppler effect (which describes how a wave changes frequency relative to the movement of an observer) and mathematical analysis so they could work out where the plane last was before it crashed. It was suspected the plane was above 30,000ft before it crashed.

The challenge now will be finding the black box before the battery runs out for the device in the black box that “pings” its location. If they do find it then the challenge of recovering it from such great depths in sea that has strong currents and dangerous weather.


Ruby Constants

I was doing some refactoring recently and made some interesting discoveries about how constants work in ruby.

What tripped me up was how much of a mess this code made:

Can you spot the problems here? The first obvious one is merge with a bang, and the other I will explain below.

What is a constant?

Constants in ruby are anything starting with a capital letter. So class names are constants as well as all capital variables.

Constants aren’t completely similar in other language. For example in Java and PHP you cannot re-assign or change a constant. In ruby you can:

Now you do get a warning, but its not an error and will not stop you from continuing. In the first example, the merge! actually modified the original constant so this was applied too all other classes using this constant.

There is one thing you can do if you want to ensure that the Object that the constant holds will not be modified, and that is by using the method freeze.

But as you can see below, the Constant is still able to be re-assigned (but still gives us the warning).

As pointed out by Andrew, you can freeze the class constant that an object refers to which will stop it from being modified.

Some other useful things about class constants is how easily they can be accessed. Constants defined in a class can even be reached without creating an instance of the class. You can even dynamically call the constant if you have a reference to the Class variable that contains the constant. For example:

Im still not certain on how useful constants are for settings that never change but I prefer using them to class methods that re-define hashes every time they are called or yaml files. They can sometimes make testing easier and other times harder.

Do you use constants much and how do you use them? Reply in the comments below.

A story from Guild Wars 2

I really enjoy computer games, its my main form of entertainment in the evening and a great stress reliever. I get so much enjoyment out of it that I was inspired to write a short story from my perspective and I thought would share it with you. This is from Guild Wars 2 (some references are very game specific).

I said my goodbyes the Militia on TeamSpeak and dropped my WvW Commander Tag, just after having defeated a Sanctum of Rall zerg.

What a good night, I thought.

A few weeks ago I got excited about some changes to WvW that I had missed and wanted to check them out. I had been enjoying the new Living Story content but wanted to do something a little different. I jumped into World vs World and the first thing I noticed was that on the Borderlands the lake is gone! This looked like a big change, but on further inspection its quite simple. Now in the middle of the map is a set of capture points that allow a server to get the upper hand if they capture 3 of the 5 points. I was told on TeamSpeak that this was to replace the old orb system.

I have been venturing onto BlackGate TeamSpeak server recently and its not as scary as I thought. I joined the channel for the borderland map I was on and I was pleasantly surprised by the commander which was on this same map. His name was Cuddlepie and had a good manner, running alongside him we ventured into enemy territory. We faced a big zerg almost twice our size that I thought we woudlnt be able to take on. Cuddlepie kept his cool and issued commands, like “head to the left then split them in half”, “take it to their backlines”. With his focus and strategy we where able to take down the entire zerg. What a rush!

We continued on and took several towers and keeps. I had a really good night. Later in the week I joined again and where greeted by another commander from a guild called KING and he had a different game plan. We defended garrison from several attacks and got lots of badges. There feels like they is always some action and some really good commanders that I feel like our strong ties with BlackGate WvW community are such an untapped resource.

Tonight, I jumped in and we where getting smashed by Jade Quarry. The commander tonight was Stellioz. I noticed the he has a philippines accent (I didnt know for a long while) and got really excited about WvW and showed some great skill in open field battles. The night continued on and I really enjoyed by time. He had to grab some dinner and we where left without a commander. I decided that I would give it a go, even though I didnt feel completely confident that I knew all of the current intel in WvW, I did it anyway as there where 5 Resonance online and in BlackGate TS so I thought “Why not?”. We where outnumbered and we took a few camps. We tried some things like some ninja trebs but the scouts where on to our plan. Later we defended our Tower several times against both Sancum and Jade. We responded to a Treb and took it out and saved our tower! Just as we saved our tower from an invading group we spotted a zerg bigger than ours.

We didnt have many numbers at this time in the night and I wasnt sure we where able to take them on. We tracked them to a chocked point. Then we engaged them once and they rallied behind the tower. Just as things go quiet, Stellioz came back from dinner and jumped in too. We spotted another group, this time bigger than the last! Stellioz said we could take them and offered some comments on how we would engage them.

We where off!

Running right into the thick of the battle, dropping some fire for might and blasting it on our way through. We focused the back lines and taking out the elles and the necros. A few of us dropped and our warriors threw down banners getting our downed up. It finally looked like we would win! We cleaned up the rest some backing and frowing and we took out the zerg.

The end.

The mystical “Tableless” Model for rails

I had a bright idea for an improvement for the app I am currently working on. I have a Rails model that has only 2 rows in the database and the data is never changed, it has quite a lot of relationships and is used quite extensively. My idea was to remove the database out of the equasion and create a Model that would be entirely self dependent. Here I will show you my methods of maddness:

My Startup Weekend in Adelaide

I recently attended startup weekend in Adelaide. I was really excited because it would be a good opportunity to build something and test my new skills I had learn recently programming ruby (and using rails).

The weekend started on Friday night where attendees had the chance to pitch their ideas in a quick 1 minute pitch. There where some exciting ones. The ones that stood out for me was a pitch about a rating system for health care professionals , a iPhone app to find and rate pubic toilets and a coffee rating app.

I decided to join the team doing rating and listing of health care as I though this would be really useful for lot of people if it was successful and seemed like something I could help and build a great MVP.

On the Saturday, after having formed our team on the Friday night, we where all ready to go. We had a really large group which became quite hard to manage at the start, we decided to break the group up into technology (MVP) and business so we would all.

In creating the initial product and setting up a starting point I made a few mistakes with setting up my project. Something that I found really useful and saved me a lot of trouble, was a ruby gem called Composer. It gets a project up and running really quickly by asking you a series of questions of things that you might want to add to your project, like Devise for auth and Twitter bootstrap.

Wanting to get something up and running really quickly, Luke in our group recommended Heroku. I had never used it before but wanted to give it a go. I was really impressed! All you need to go is install the Heroku gem on your computer, run heroku init on the project and then type git push heroku and it is deployed in a matter of minutes! It runs bundle and install all of the gems in the gemfile and then spins up a server.

One of the elements of the weekend that I really enjoyed was the mentors  that came to each group and provided insight into the various aspects of our project and how we could improve. This was really interesting to hear.

I made quite a bit of progress with the app and by the end I was able to make a simple way to lookup a directory of health care professionals. We used the app in our pitch at the end of the weekend. The other pitches where really good to watch and to see what all of the other teams had come up with over the weekend.

PHP Debugging

Recently while working on a particular project we had some strange errors that came up. In the usual php like way, the error messages where not very helpful and didn’t allude to what the actual problem was. Have you seen an error like this before?

Headers Already Sent on line 392

Invalid argument supplied for foreach()

Well, this handy functionality that I discovered in php made my life easier.

var_dump(debug_backtrace()); die;

I placed this right before the code that was complaining, and it allowed me to see where this function was being called from. I was then quickly able to fix the problem and be on my merry way.

Yii and Authorization Rules (RBAC)

One thing that has recently puzzled me with my discoveries of the Yii PHP framework is to do with how authentication rules work. (In particular RBAC)

I have been recently working on a big project for work that is quite complex with quite a few different systems that are all connected to each other. It also supports different devices, like the iPad and the iPhone. For this app it is important to restrict a user to a particular set of assets (that they create) and share these with other users in their company. To implement this, I stared by looking into the standard approach recommended by Yii documentation and that is the Authorization Component. On the implementation level, You have the option of either doing rules based on a file (CPHPAuthManager) or using a database backend (CDbAuthManager).

After doing a bit of research, using the CPHPAuthManager for a large application is a bad idea. Everything is stored in a file and every time a access rule needs to be evaluated, this file will need to be checked. Also all role assignments are stored here, so if you have a large amount of users then there will be a lot of information stored in this file. This would make it unmanageable and wouldn’t scale very well for bigger applications.

It appears that CDbAuthManager would be the solution. What it does is store all of your access rules in the database. The auth manager will generate a pre-built database schema that Yii provides. The first thing I found was the component that is available in the Yii repositories called yii-rights. This extension allows you to manage your Authorization within the app and create access rules as needed. The problem with this is that its all held in the database and I would be deploying this application to the production server after testing it on the staging server and did not want to have to create access rules in both instances and have to update them every time we made any changes to the rules.

This is where we came up with the idea of using a console command to create all of our access rules. Yii has a great system for creating console commands you can run on the server, if you want to learn more, check this out. This console command creates all of our access rules when run and stored them in the database so that we can version control our access rules and have them in one central place.

The problem I have with out current system is that the access rules have business rules that are evaluations to check the current user against the part of the system they are trying to access or modify. What I dont like is that the access rules are in the database and then eval’d by php at runtime. Now this is a bad idea and most in the php community would tell you to stay away from eval for most things. The question I have is, is this used well or could it have been done better?

If you have something to share that might shed some light on what I might have missed or another way to do it, post your comments below or post to my question on stackoverflow: