Sometime ago while using method_missing to implement some functionality I got the weird behavior that it would work only most of the time but not always.
In retrospect it's now pretty obvious but in the heat of the moment it took me about half a day of investigation and talking before I figured it out.
What happened is that I did only half of the work.
I defined method_missing but I forgot to define respond_to? accordingly.
The result is that it worked when I called it directly on the instance, but failed if an association was involved.
To give an example, say you have a class like this:
class A < ActiveRecord::Base def example true end def method_missing(method, *args) if method.to_s =~ /example/ example else super end end end
Calling *example* directly on your instance works just fine.
>> A.new.my_example
=> true
>> A.create!.example_me?
=> true
All fine, but as soon as you get an association in the middle of things:
class B < ActiveRecord::Base belongs_to :a end
It just doesn't go well anymore:
>> b = B.new(:a=>A.new)
=> #<B id: nil, a_id: nil, created_at: nil, updated_at: nil>
>> b.a.example?
NoMethodError: undefined method `example?' for #<A id: nil, created_at: nil, updated_at: nil>
from /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:220:in `method_missing'
from (irb):55
>> b = B.create!(:a=>A.create!)
=> #<B id: 4, a_id: 7, created_at: "2010-03-08 21:15:01", updated_at: "2010-03-08 21:15:01">
>> b.a.failing_example
NoMethodError: undefined method `failing_example' for #<ActiveRecord::Associations::BelongsToAssociation:0xb70491d0>
from /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:220:in `method_missing'
from (irb):57
Now, this last error is a bit clearer but I don't remember running into it at the time.
If I had just followed the association_proxy:220 hint right away... ;)
What happens is that b.a doesn't return the instance but rather an AssociationProxy instance that provides ActiveRecord's extended functionality and this proxy relies on A#respond_to? to correctly forward method calls to the actual instance.
What I should have done is:
class A < ActiveRecord::Base def example true end def method_missing(method, *args) if method.to_s =~ /example/ example else super end end def respond_to?(method, include_private = false) if method.to_s =~ /example/ true else super end end end
>> B.create!(:a=>A.create!).a.example?
=> true
>> B.new(:a=>A.new).a.failing_example
=> true
There are some much better write-ups on this topic, if you want to read more:
Using method_missing and respond_to? to create dynamic methods
Solving the method_missing/respond_to? problem
Now, I must be honest here: what I was doing was a big code smell :)
It taught me the lesson to use method_missing properly and was even quite fun to debug and all that but what I really needed and end up doing in that case was a group of delegates here and there and voilĂ , it was all cool and clean.
Labels: meta programming, ruby on rails
Remote Pair Programming
A few weeks ago, Larry O'Brien and I set out to experiment with Remote Pair Programming.He wrote about it on his SDTimes column: Windows & .NET Watch: Problematic pair programming
We both liked the experience and would be rather happy to be able to work on a long term project that way (anyone hiring a remote pair?), but since his column gives more emphasis to the down sides, I thought I could write my point of view which is more on the up side ;)
In general, I agree on the down sides but I got so excited with the advantages I saw that I would barely remember mentioning the problems if asked.
The focus
Working in pair forces you to focus on the work.
There is just no way you can be there and not be paying attention to whatever it is that you're supposed to do.
You get so focused in the work and the communication that the environmental noise that would otherwise annoy you just seem to disappear.
The speed in which you make progress can vary with a lot of factors but you are *guaranteed* to make some progress. All. The. Time.
The challenge
The fact that someone is watching your every step means that you keep challenging yourself to do things the best way you can.
You just can't look sloppy to your pair.
That means you avoid cutting corners or knowingly doing the wrong thing out of laziness or "to fix it later".
If you do something that feels wrong, you'll be called on it and have to defend your point, so, unless it's worth the trouble, you just work a bit harder and go for the right thing.
The safety feeling
You certainly feel more confident about every line of code you write.
Anything absurdly wrong will surely get catch by one or the other.
Even the small errors or typos get spotted really fast.
Sure, things can still be wrong, bugged or bad designed but the fact that the code was co-created and reviewed by at least one more person gives you a comforting feeling.
The learning
Working with someone else full time means you're continuously learning or at least exercising a different approach than your own.
You learn new tricks.
You get to defend your points.
And you question everything.
At the end of the day, you feel actively improving in many things.
Even your English, if you're not a native speaker ;)
The loneliness
Working remotely means you're almost always alone.
When your clients are in different countries, thousands of miles away, that's guaranteed.
It's just too expensive, time consuming and a tiresome task to travel for a meeting.
If there is something I miss every now and then it's having someone at close reach that I can share work stuff and work together.
Working in a pair was a comforting step in that direction.
The results
We shared the resulting project on Github.
If you'd like to poke around, it's available at http://github.com/carloslima/pasteme
Overall, I consider it was a big win over the solo experience.
There is only one thing that could be much better and that's the IDE/Tools support.
While sharing a VM using VNC mostly worked, it didn't come close to the experience you get when you use Saros, Bespin or even Google Wave.
Being able to use all your monitors, drag windows around and organize it the way you like better would definitely improve the experience.
Have a different opinion or want to suggest a different tool?
Just drop me a comment!
DHSnapshot
A while ago I needed to find a good setup for backing up some machines.I wanted to use RSync to do snapshots-like backups.
This setup is:
* Off-site
* Storage efficient (the bulk of my backup was unlikely to ever change)
* Network efficient (only transfer changed files)
* Keeps versions going back a few months
Now, DreamHost offers 50GB of free space for personal backups and that seemed a good fit for this particular situation.
Then I found RSnapshot and it was exactly what I needed.
Unfortunately, RSnapshot doesn't backup to an external server.
It's meant to work in a setup where the server hosting the backups connects to your data sources and pull the files.
That totally makes sense and is probably the best setup but it wouldn't work for me.
DreamHost doesn't have RSnapshot installed on their backup servers and the only access users have to that machine is SFTP and RSync.
I needed something that would work with just that.
I came across a post about backing up DreamHost websites to DreamHost Backups that showed a workaround to the lack of SSH access to the backups service.
So I took the idea, mixed it up with the stuff I wanted from RSnapshot and wrote a small perl script to do it.
It's called dhsnapshot and is published at GitHub: http://github.com/carloslima/dhsnapshot
At this moment, it's not very flexible: it's hardwired to keep 7 daily, 4 weekly and 6 monthly backups and it's also limited to a single backup source (you can only point it to one source directory)
But it's not hard to change, or even make it configurable.
I might do it if I ever get the need or motivation :)
One way or the other, it should be reasonably simple to setup (instructions on GitHub).
I'd be glad to know if it helped anyone.
So, please, drop me a comment if you fnd it useful.
Subscribe to:
Posts (Atom)