A small question popped up while I working with rails today, I’m not sure how well this is documented and it seems fairly straight forward but I feel the urge to share it.

The question is this: how can I route using, instead of a unique id, a different column in a set of data.

Say, for example I have a table with the columns id and name and both are unique. The data contained in my table is:

id name
1 Fred

The default routing would be as such:

http://localhost/users/1

But what I am after is this:

http://localhost/Fred

How is this done? After playing around here is what I came up with:

Open the routes.rb file and add the line:

map.connect ':name', :controller => 'users', :action => 'show'

So we are passing the parameter :name to our show action in the user controller. Now we need to modify our users_controller.rb to accept this parameter.

def show
@user = User.find_by_name(params[:name])
end

And that’s all there is to it. Look up the Ruby on Rails API for more information on the find_by_ function

I haven’t had much experiance with ruby on rails so if I have said something that may be a little misleading make sure you let me know!

Yes, like every other human being who has come across Ruby On Rails there is a strange temptation to start playing with words to show how clever we are but I’m fast to realise that its not the only thing you can do with Ruby On Rails, much to my relief.

If you have no idea what Ruby on Rails is see: http://rubyonrails.org

Having been introduced to Rails many moons or perhaps about 2 years ago, it had always remained concealed behind a cloud of mystery for myself and as I developed websites for different clients using mainly PHP scripted Content Management Systems I didn’t feel the need to waste time and learn how to use it.

Silly me?

Perhaps so, though at some point there comes the urge to write a new system alot simpler than those systems such as Joomla! or others like it, a system that is tailor made to clients needs and not overloaded with features that have no relavance or are perhaps useless.

Time to removed that cloud of mystery … Enter Ruby on Rails.

Setting Up Rails

I currently am using Mac OS X Tiger 10.4 so I set out to find information on setting up Ruby On Rails.

A brilliant tutorial which has pretty much got me set was from hivelogic:

http://hivelogic.com/articles/view/ruby-rails-mongrel-mysql-osx

Though now with new updates and other current changes there was a few things that triped me up.

  • In the Paths section of the tutorial you will see that you are to edit the .bash_login file so that terminal looks in our /usr/local directory though I found when I entered ‘ruby -v’ into terminal it was still showing my old 1.8.2 version of ruby. After scratching around some time on the internet I found that I would actually need to edit .bash_profile instead of .bash_login for terminal to recognise a new path.
  • When installing the MySQL native bindings gem using the gem install method it was downloading yet failing to build, again I scratched around the net for a bit and found that due to Ruby Version 1.9.1 I would need to get the latest version of MySQL/Ruby and for some reason gems was only getting version 2.7 and the latest is 2.8 .1 (http://rubyforge.org/frs/?group_id=4550) so I downloaded that tar.gz file and installed it, tested and everything was looking great

I haven’t bothered installing a http server for Rails yet as I am just using the one packaged.

With everything installed, the fun begins…

More on Ruby On Rails comming soon!

Boring old black and white text scrolling down a screen isn’t very attractive, right? (Refer to my previous post). So with this on my conscience I set out to give my ruby program, which gets data from FlightGear, a nice pretty interface… well sort of pretty.

So… here it is!

Now when dealing with constant processing of input or output data and in my case it is data being received, as well as a graphical user interface you are asking ruby to do two things at once. In the general scheme of things it is not what simple old ruby does, all it knows is to run through a series of instructions and then either loop back over those instructions or terminate, that being said with exceptions. So let me now introduce threads.

Ruby threads, without going into to much detail, allow multiple processes to run parallel to each other. So I needed ruby to not only be managing the GUI but also receiving and processing I/O data and thus the use of threads.

Setting up a thread is quite simple:

And you can find more information about them in the ruby core libraries documentation, see here.

Like the rest of my programs featuring GUI’s I have a soft spot for FXRuby, and I really don’t know why but I think it is mainly because it works, strangely enough. To allow me to update each piece of information (speed, altitude, etc.) I used FXRuby’s Data Targets which I setup to be updated from within the threaded receiving process.

All in all it is a pretty straight forward program but there are still plenty of loose ends needing to be tied up before I can call it a completely finished. It doesn’t even fulfill its intended purpose yet so there is plenty more to do.

If anyone is interested in the complete ruby code post a comment (with your email address) or send me an email and I will forward it on to you.

Stay tuned for more!

Ruby, IronRuby and FlightGear; Three big subjects in their own rights but here I am mixing them all together and perhaps getting into things a little to much over my head.

The idea of using these three programs is to get real time data from a flight simulator program and then process and output it through ruby. Also a future goal is to be able to send data  to control different systems while in flight through external components.

The following post contains information on FlightGears .xml protocols and configuring the simulator to output data through a socket. Also two scripts in Ruby and IronRuby which gets data from FlightGear and displays it.

Please also note that you will find that some information I give is similar to what is given at http://linkslink.wordpress.com/takeoff/ as I used the information as a guide when I was learning about the .xml Protocol files and has led me to some handy .Net libraries when using IronRuby.

Read the rest of this entry »

My apologies to all you fanatic readers out there who have been waiting for an update… (I think I am talking to an empty room…), but oh well, here is an exciting update! WAHOO! (insert rolling eyes here):

Inspired by Mr. Ashish’s little project (ashishrd.blogspot.com) of controlling his remote control car via the computer, I decided to try to the same to my remote control car and hook up my wireless camera to it as well.

So I guess it all started with my brothers remote controlled car that was laying under his bed collecting dust.

From there the idea evolved into a question of can Ruby programming language be used to control the car? Well of coarse it can! I mean, its Ruby we’re talking about here. Though the initial search on the subject seemed to turn up nothing in particular, I was mainly wanting to know how ruby when about detecting a key press. So search I did until I found this peice of code:

So it was a start… from there I just mucked around with it until I got it to eventually work. I then wrote up some if statements to check weither or not the key was pressed and if it was what the program was to do, so for example if W was pressed send 0001 to the parallel port. Now for an interesting fact: I have always heard people, when talking about programming, discuss effeciency of the programs design and how much fast the program can run if it is structured in a certain way, now I had never came across that in my mild dabblings in programming until this day; With my program all written up I then tested it by pressing up, down, left and right but there were slight pauses in the program and it would detect the left key unless you pressed it a few times and the same with the right key and I had a weird feeling it was to do with the if statements… so I changed it all to a case statement and everytime no matter how fast I pressed each key and in what order each keypress was detected… very interesting!

Now that I had that under control, the car’s controller had to be interfaced with the the computer. I happened to have four relays laying around at the time so I used them and found a nice little schematic that helped things, I just modified it to my needs:

Also instead of the BC548 I just used 2N222A’s and I didn’t bother with the resistors… everything seemed to work fine without them… this is how the whole thing turned out:

And here is a picture of the RC Controller cracked open and a few wires soldered here and there:

And so after a successful afternoon of soldering and programming it all worked fine… here is the car with my camera attached and ready to allow me to experience the great outdoors from my computer… oh I am lazy!

I recently had a weird urge to make something irritating or something that was in some small possible way, annoying. This then lead to a train of thought that finally rested upon: is there an easy way ruby can output a sound/beep at a certain frequency?

Shortly after the thought… the answer become clear, yes!

I found a post on a forum, basicly giving me all the info I needed to start. Here is the link:

http://www.ruby-forum.com/topic/74725#112277

Here is the code:

N.I.T Code

beep 900, 1000 == beep frequency, duration

By changing 900 the frequency of the output is changed and by changing 1000 the duration of the output is changed.

I then went and attached a nice and pretty GUI to it all:

N.I.T GUI

If your interested in the code behind it all just send me an email!

Apart from that, a fun little project and completely useless!

LED Dice

April 21, 2007

Well I have still been playing around with the scanner but in the mean time I done this quick little project today… LED Dice.
LED Dice

Exactly the same wiring as the LED’s that I hooked up back in my second post and I stripped a floppy cable down to only 8 wires and used that to connected the circuit to the parallel port. Then I wrote up a little script/program thing in ruby that randomly generates the number to send to the parallel port.

LED Dice Roller Program

Again, if your interested the script just email me so I can send it to you… apart from that its not going to be very useful for me but still fun to make it work.

I hope to have some more little projects coming up soon so until then stay tuned!

I finished my LED Controller Program (written in ruby) today… Woohoo! Now I can move onto something new… anyway, about the program:

Its very basic and anyone with a good knowledge of the ruby language can probabley see that the script is a bit messy but the main thing here is that IT WORKS! I feel so proud! … :D … Moving right along… Here it is:

LED Controller written in ruby

If anyone is interested in getting their hands on the script just send me an email (zcvohland@gmail.com) and I will send it to you.

Well, like I said, now I’m going to move onto something new… I want to experiment on the scanner and try and control its stepper motor via the parallel port…

I didn’t get time to do much today but I decided I will try writing my own program that can control the LED’s that I rigged up to my parallel port (see last post).

Ruby is my programming (scripting) language of choice because it doesn’t require to be compiled, can be written in a notepad and run through command prompt (on windows) and thats all there is to it, also it takes a lot of work out of written a script.

Anyway I checked out the net to see what I could find on using Ruby to send commands to the Parallel port and there is hardly anything! And by anything I also mean general discussion of how to do it as well as documentation.

I came across this website:http://yueda.issp.u-tokyo.ac.jp/weda/ruby/ruby.html

The guy that wrote it had written a ruby script that enables us to control the parallel port in windows… Perfect! (I think there is a similar script out there for Linux, but not sure for Mac)

So I downloaded the script (prnprtLX.rb) and also had to download a DLL called diportio.dll which I got from http://www.ln.com.ua/~real/avreal/descr_e.htm

I then wipped up this code, pictured below:

The part circled red is the data you send through the parallel port, so 255 is in binary 11111111 which turns all the LEDs on… if you change that value to 0 it turns all the LEDs off… if you change that value to 52 (in binary: 110100) then only the first two LEDs and the forth LED is turned on.

So thats it for now… tomorrow I think I will try and write a few different things for the LEDs and see what I can come up with… perhaps a GUI would be nice?