|
|
|
SearchCategories
Books by the AuthorTwitter AccountOther Ruby Projects |
Code as a Data Type
Posted over 6 years ago
in Ruby Tutorials.
IntroductionThis is the first of a series of articles where I will try to demystify some Ruby idioms for the people who come to Ruby through Rails and find themselves wanting to learn a little more about the language under the hood. Strings, Arrays, ... and Code?You don't have to code for long in any language before you get intimately familiar with some standard data types. We all have a fair grasp of Ruby's Allow me to explain what I mean, through an example. First, let's create a little in-memory database to work with:
Of course we need to be able to query this data. Let's add a very basic query routine:
Finally, we should be able to make some queries:
We have the beginnings of a system here, but my queries aren't very powerful yet. Let's add support for a single boolean operator:
The good news? We can now enter queries like:
But there's a lot of bad news too:
It's clear that what we really need here is a complete language. But wait, aren't we already using a language? What about Ruby? This is the line of thinking that leads to Ruby's code blocks. Any method in Ruby can have a bit of code included with the call. That method can then yield to that bit of code, pass it parameters, and even get a return value from it. Let's rework
In my experience, you know you're doing Ruby right when you are dropping code and gaining functionality. So how did we do? Check out these queries:
As you can see, we now have a full language at our command. We have a complete range of operators, access to goodies like Regular Expressions, and we can easily query the Array sub field in any way that we need. LambdaAll this block stuff is very handy, as you can see. Soon after you start using them, you're going to think, "Now if I could just stick this block in a variable..." Enter Tools like
In this example, lambda() will just wrap up the provided block in an object you can pass around and use as needed. I've only used it to count a single database here, but I could have easily counted more if we had them. This example shows off one more interesting aspect of Ruby's blocks: They are "closures". Uh oh, ugly CS term alert! It's not as complicated as it sounds. Relax. See how the Putting it all TogetherI bet you're wondering what all this has to do with our database example. Let me tell you... Ruby has one more shortcut for blocks. You can automatically have them wrapped up into an object (or even unwrapped!) at the time of the method call. In other words, I could have written select() like this:
That funny Now that didn't change anything for our
I'm only using one new trick here: In Since we defined an
That ends my tour of Ruby's blocks. Remember, the easiest way to let all this sink in is to tell yourself that code is just another data type in Ruby. Don't be too surprised if that gets you solving problems in a whole new way... |
|
|
|
This is almost exactly the words I used to describe the refactoring process in Ruby to my employer.
Great Article James! :)
Looks like you have the beginnings of a mock database system here. You know, we could really use that for the DBI refactoring effort.
HINT HINT
Dan
Daniel: See KirbyBase for a much more complete example.
Hey,
I am here 5 years late... It looks like I have a lot of reading to do. Great explanation of the blocks btw.