Singleton and Observer Pattern in Ruby 1.9

Ruby — Dillon @ 10:29 pm


This post isn’t specific to Ruby 1.9, I just want to differentiate this a little bit from other examples out there.

First cut of singleton

A while ago, I wrote a singleton class as a proof of concept. It turns out that this was the hard way. Since then, I’ve just used the Singleton Mixin instead of writing it by hand. For this post, I’ll talk about both. First off, what we’re going to create is a God class which, other than starting a quite dangerous religious debate, will illustrate a singleton object and how to work within and outside it.

The first example is very contrived. We’ll create a God class and define a metaclass with class < < self which will keep us from creating a God instance with God.new and instead will force use to refer to only one God. So essentially, any properties within that block will be constrained to one single instance. Anything outside it, is a normal instance.

Like I said, this is pretty old code and so it doesn't flow all that well. First, we create the God class as a singleton because we'll just declare that all religions are about the same big guy (as an example). It has instance variables like name, religion and soul because God doesn't have one name. Everyone calls him something different. This is also true for religion. Soul as an instance variable of God actually doesn't make much sense. But it does allow us to track what "humans" there are. In the next example, this will be clearer.
(more...)

Perl vs Ruby

Development,Ruby — Dillon @ 12:18 pm


This isn’t like Perl vs Ruby: FIGHT! This is more like walking vs running: COMPARE!. They will both get you there, just in different times and sweat amounts. :)

I stopped doing Perl in about 2002 but not because I hated Perl. I stopped doing Perl because started learning Java. Java was where the jobs were. Since all that, I know I’ve become a better developer so a lot of this is situational to me and not a reflection on the language. Without a control group, you can’t say that the language has made me any different. I’ve learned things (not all things) from each language, environment and community as I’ve bounced around. No doubt, I’m Ruby-biased so please post comments, corrections and better ways of doing things in Perl so that I’m the most accurate I can be. Recently it’s popped back up at work and I needed a refresher but

Ok, enough about that. I’m here to compare how you do things in each language. Ruby is all about blocks, syntax sugar and a very different community than Perl. Both share many idealistic values (such as there’s more than one way to do something) and Ruby was inspired a lot by Perl. So this isn’t a battle but more of a Rosetta stone.
(more…)

Watchr filesystem events not firing

Mac,Ruby — Dillon @ 10:30 pm

I wanted to use a ruby environment inside a Ubuntu VM but still be able to edit text files in Textmate. So I shared out my project folder and fired up a watchr script I made. Unfortunately, it didn’t work at all.

I tried a few different libs, including rev, rb-inotify, rb-fsevents (which turned out to be mac only) but nothing was firing when I’d save a file. I thought maybe rev or watchr was broken in 1.9 but that was not the case. The problem is the vmware shared folders. When you do a write, modify or whatever, it doesn’t fire the same hooks as a local event does:

Modify File -> Textmate Save -> project/foo.txt (does not fire in watchr)
Modify File -> Vi Save in VM -> project/foo.txt (watchr fires)

And it wasn’t just watchr (as I said), every ruby library was seeing the same thing. So I gave up on the shared folders through VMware and just installed netatalk.

sudo aptitude install netatalk

I can edit files in Textmate this way and watchr works as expected. So why is this important? Because a tight REPL is important. And watchr / wtchr makes the tight loop happen. This is also probably going to happen if you use autotest in a project with lib/* test/* too.

Pretty specific but I hope it helps someone.

Arduino Cat Faucet with Mongodb and Rails

Arduino,Noteworthy,Rails,Ruby — Dillon @ 11:01 pm

I built a robot arm for my cat during a month-long geekcation. :) Here are some shots of the web interface. The graph shows the percentage of the day that she drinks.

Final hardware rig

Background

My cat likes to drink fresh and cold water directly from the faucet. We get up and turn on the faucet only to leave it running after she’s jumped down. It’s not really a big problem for us but I saw a fun problem that I could work on. As much as this seems like a weird and freakish oddity, it’s a potential start of a smarthome sensor network that may provide some utility. I also saw an opportunity to learn various things such as MongoDB, mechanical construction with Microrax, Rails3 and more development on Arduino with an Xbee module.

(more…)

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2012 SQUARISM | powered by WordPress with Barecity