Content
Filed under
ruby
In a scenario similar to this; a base class, Animal with generic method move which is specialized in descendant classes, with real functionality.
class Animal
# Overwrite this in your animals.
def move;end
end
class Bird < Animal
def move
fly
end
def fly
puts 'flap_wings'
end
end
Certain conditions have to be enforced before an animal (any of them) can move. For instance, no one wants a bunch of zombie animals moving around. I had to make sure every animal instance will only move if it is alive. The straight forward way to do this is: raise an exception in the specialized method.
class Bird < Animal
def move
raise 'Zombies do not exists!' unless alive?
fly
end
def fly
puts 'flap_wings'
end
end
That will work, however as you can see it’s not very ( if at all) DRY.
Using some meta-programming in the base class, it is possible to maintain a syntactically generic method (eg. move), and enforce some restrictions before the children class specific logic is called.
class Animal
def initialize
# reopen the class in the constructor
class << self
# Alias the existent move method
alias :unrestricted_move :move
# define a new move method appliying restrictions
define_method(:move) do
raise 'Die zombie die!' unless alive?
unrestricted_move
end
end
# Overwrite this in your animals.
def move; end
end
Now you won’t have anymore death animals moving around and you can still safely call move in any class descendant of Animal.
geekvicious »
2008-03-26 »
Comment |
Permalink
Filed under
programming + ruby
We’ve using merb + datamapper lately at work. Since data mapper 0.3.0 was released recently I decided to upgrade my own branch today. After updating the gem I ran my tests…just to get some very strange behavior. NoMethodError exceptions from trying to access the method ’suit’ in classes that had nothing to do with my tests nor the classes I was testing (testees?).
After hours minutes of frustration, I tracked down the exception to ruby’s Test::Unit::Collector, which relies on the standard behavior of the < operator between classes (A < B if A child of B). It turned out that data mapper 0.3.0 monkey patches Class, mixing-in Comparable and overwriting <=>, as a side effect Test::Unit becomes clueless about which classes are tests and which are not and starts calling ‘.suit’ in every single class in the ObjectSpace.
Well, after all monkey patching might be destroying ruby.
geekvicious »
2008-03-20 »
3 comments |
Permalink
Filed under
defensio + programming + python
PyDefensio is small python module, that brings all the Defensio spam-filterig power to your python applications. Don’t forget to read Defensio’s api documentation first, more about PyDefensio, and download here.
admin »
2007-12-06 »
7 comments |
Permalink
Filed under
bash + linux + programming
This won’t give you a real random, but will do when you need a not trivially predictable number:
~$ expr $(date +%S) % 10
geekvicious »
2007-10-23 »
2 comments |
Permalink
Filed under
linux + programming + ruby
I’ve been using aptana’s RadRails Eclipse plug- in to develop RoR apps for over a year, it has lots of nice features and has been working fine for me, however it is resources consuming, and heavy. in one phrase: RadRails is a nice IDE although far from perfect.
Probably, because of its architecture Eclipse is not very compatible with the Unix and Rails approach: ” Do one thing and do it right” , instead it does everything, and works well most of the time but, still feels awkward when developing rails.
Lately I rediscovered Vim probably the best Unix editor ever ( no matter what emacs fans would say ) and a really nice and small plug-in , that makes rails integration immediate and unobtrusive. It includes syntax highlighting, auto completion, easy rails navigation, Rake integration… and some more features.
geekvicious »
2007-08-29 »
3 comments |
Permalink
Filed under
linux + programming + ruby
Being a big KDE fan I was glad when the people from snailbyte came up with KDE desktop notifier for autotest. Even though KDE is by far my favorite desktop environment, knofity has a big drawback; it is not possible to choose the color, for the notifications. So based in the growl autotest notifier, I wrote a version for osdsh an X OSD manager, it is not as pretty as growl ;)… but it gives me colorized desktop notifications on linux.
To use it just make sure you have installed osdsh, and it is running:
me@my_machine$ osdsh
Then put this in your .autotest file:
module OSD
def self.osd msg, color
system "osdctl -e \"dset(C,#{color})\""
system "osdctl -s '#{msg}' "
end
Autotest.add_hook :red do |at|
res = at.results[/d+ tests.*$/]
osd res, 'red'
end
Autotest.add_hook :green do |at|
res = at.results[/d+ tests.*$/]
osd res , 'green'
end
end
geekvicious »
2007-08-23 »
1 comment |
Permalink
Filed under
kubuntu + linux
I recently got a new laptop , a dell Inspiron 1501. Having a 64 bits processor, it seemed to me kind of wasteful to use a 32 bit OS, so I took the easiest road, a 64 bits Kubuntu install; all the hardware worked out of the box except for the expected Broadcom’s wi-fi problem.
But when it came to install Skype, well not so painless. I need to use Skype for various personal reasons, so I *had* to install it.
First I tried using linux32 and the debian package provided by the Skype’s download page, it just wouldn’t… since Skype 1.4beta uses QT4. Then I tried with the static version, same story, it kept complaining about libsigc2 not being found even after I installed all the versions reported by aptitude.
After some hours of frustration I found getlibs a little application that did everything for me in one command
getlibs skype_static-1.4.0.74/skype
and voila.. it just worked!
geekvicious »
2007-08-21 »
Comment |
Permalink
Filed under
random + religion
There aren’t many more experienced censors than our good old catholic friends, for centuries the Holy Inquisition burned at the stake whitches and heretics in order to purify the faith.
They also occupied a good deal of their time controlling the public opinion, baning and burning impious books. Thankfully except for the most backwards societies they can’t do that anymore; however they still try to manipulate the public opinion as much as they can.
Their last attempt: wikipedia , a usual they blame everything on some “bad guys” using the Vatican’s computers. Hard to believe knowing who is in charge now.
geekvicious »
2007-08-18 »
Comment |
Permalink
Filed under
programming + ruby
For those who are developing web apps using RoR, debugging has always been a pain . It is much more easier now using the ruby-debug gem.
- Just gem install ruby-debug
- require it in your environment.rb
- Insert debugger in your code wherever you want the app to sop
The real guide here
geekvicious »
2007-08-17 »
Comment |
Permalink