Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts

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.