Tuesday, March 1, 2011

Using class_eval to add methods to a class

So I finally slogged my way through Chapter 10 in Beginning Ruby. It wasn't nearly as helpful or interesting as I thought it would be. I mostly got hung up on MySql for a few days and had a hard time working through it.

I don't think I need to learn how to interact with databases just yet, so I moved on to Chapter 11: Advanced Ruby Features. Aha! Now we're talking. I spent some time working with class_eval, which allows you to add methods to a class dynamically, and exec, which allows a script to run another script.

Here are some questions I've got, though.

What's the benefit of using class_eval to add methods to a class instead of just defining those methods within the class itself? This isn't a temporary definition, right? So it seems like you'd end up writing about the same amount of code, but defining methods within the class instead of adding them later would keep the code more organized.

And when it comes to exec and fork, what's the benefit of using these commands to spread the functionality between scripts? I'm seeing this as similar to using mix-ins. For example, you could have a script that checks the time on the user's system, then either does or does not do something based on that time. It might be useful to run this script from various parts of your overall program and that's why you would want to do it. Am I thinking about this in the right way?

I'm also starting to wonder where I should go once I'm done with Beginning Ruby. The book is already warning me the last section is mostly just appendices and reference, so any recommendations are welcome.

Sunday, February 27, 2011

Getting past my MySQL gem issues

So the MySQL gem has absolutely killed my productivity today. A simple command line install didn't work. So I installed macports, only to have my friend Eli recommend homebrew instead. Then I had to uninstall macports (surprisingly difficult, it was like an infestation) and install homebrew. So I finally get homebrew installed, hoping to use it to install MySQL, then use that installation to install the MySQL gem. MySQL was installed, but I'm still having errors with the gem. WHAT!

tldr: the MySQL gem is a headache I don't need to deal with right now, so I'll skip it.

So this morning I've been learning about how to create, document and distribute gems. It seems a bit far-fetched to think I'll be writing code others might want to use, but who knows? One thing I really like about Ruby is the openness of the community: experienced developers are open to helping newbies and developers are open to sharing code that others might find useful.

It feels good to be back in the swing of things and not just beating my head against the wall. This is one of those things I told myself I wouldn't get hung up on.