Thursday, February 10, 2011

Mix-Ins and the Indianapolis Ruby on Rails Community

Last night I headed to my first Ruby meet up, with the Indianapolis Ruby Brigade. I knew a few people that would be there, but the room was packed with around 40 people with varying degrees of experience with Ruby.

There were three presentations and while most of it went over my head, I understood more than I would have two weeks ago. I try to keep things in perspective. I can't expect to know and understand everything right away, but I've definitely been making progress. After the presentations, I spoke with a few guys about Ruby and got some great input.

Everything I've been reading about how to get started learning Ruby stresses the importance of becoming part of the community, both locally and online. A local developer told me his own learning and understanding grows exponentially when he's talking to other developers. Overall it was a great experience and I can't wait to go back in March.

In more concrete news, I've been learning about mix-ins tonight. The way I understand it is like this: You build a module with a specific set of attributes. Then you can create a class and include the module, so the class has all the features of the module as well as any class specific features.

Inheritance works as a single unbroken chain, but with mix-ins, separate classes can share some of the same features. Maybe you've got code like so:


Now say I wanted to add information about Fur and Whiskers. Assume this information is the same for both Oskar and Biscuits. As Oskar and Biscuits are children of the Cat and Dog class, which is a child of the Pet class, I'd have to add the information in twice somewhere (assuming I didn't just want to throw it into the Pet class)

DON'T WORRY, Mix-Ins to the rescue! (I'm already pronouncing this as "mixin's" in my head. That may not strictly be correct.)

So what about doing it this way?


I just create two new modules: Fur and Whiskers, then use include Fur and include Whiskers to attach that info to both the Oskar and Biscuits classes. Perfect!

No comments:

Post a Comment