Computer Science Canada Tip: Iterating with an index |
Author: | wtd [ Tue Jul 12, 2005 7:42 pm ] | ||||||||
Post subject: | Tip: Iterating with an index | ||||||||
Let's say we want to iterate over an array and print out each element. That's pretty easy.
But, what if you want to iterate with an index number, for instance, so you get the following:
You might be tempted to write something like:
But there's a much easier way, using blocks.
|
Author: | Cervantes [ Wed Jul 13, 2005 7:00 am ] |
Post subject: | |
Nice tip, wtd! ![]() This made me think, though, "Does Ruby have overloading?" It would be nice to not to have to change each to each_with_index, but rather just add a new parameter (?) to the block. Or, does Ruby have overloading, just not when it comes to blocks? |
Author: | wtd [ Wed Jul 13, 2005 1:19 pm ] | ||||||||||||||||||||
Post subject: | |||||||||||||||||||||
Cervantes wrote: Nice tip, wtd!
![]() This made me think, though, "Does Ruby have overloading?" It would be nice to not to have to change each to each_with_index, but rather just add a new parameter (?) to the block. Or, does Ruby have overloading, just not when it comes to blocks? Ruby doesn't do overloading at all on method names. For instance, you can't have:
However, there certainly are instances where you can make a method work either with or without a block. The most common way of using blocks is to simply yield values from within a method to a block.
We can then, for instance, simply:
But in this case we don't really know what the value the block returned. Where would we need this and how would we get that value? We'd need it in places like the sort, collect, reject, and select methods. To accomplish this we need to explicitly pass the block to the method as an argument. We can do this since a block is just an object of the Proc class. So let's write a basic collect method.
Here we've created the method, and specified the block as a parameter. Note the ampersand. As of yet it does nothing.
Still, it doesn't do anything, but again we have a loop over the array. How do we call the block with the current array item as its parameter?
And yet, it's missing something. We haven't accumulated the answers anywhere.
There's still a problem. What if I passed in no block? In that case, the block will be set to nil, and we can test for that.
And now it works as either:
Or:
|
Author: | Cervantes [ Wed Jul 13, 2005 9:46 pm ] | ||||
Post subject: | |||||
Thanks for the explanation on blocks, wtd. ![]() I'm curious about that for loop though:
I can see that we're iterating through each element in @a, but how exactly does it work? I would assume that x is an integer that ranges from 0 to 3, rather than the strings (hello, world, foo, and bar). But then, that doesn't seem to match with your last line of code:
x must be a string for you to be calling the upcase method. So, your for loop iterates four times, and the value of x is the particular element of the array. If I'm correct in my deduction, that's neat. ![]() /me loads irb ... Oh man, that's sweet. ![]() I was just checking out the Ruby Library Reference and noticed each_with_index is listed as a "mixin". It also seems to be enumerable. ![]() |
Author: | wtd [ Wed Jul 13, 2005 10:12 pm ] | ||||||||||||
Post subject: | |||||||||||||
Cervantes wrote: I'm curious about that for loop though:
I can see that we're iterating through each element in @a, but how exactly does it work? I would assume that x is an integer that ranges from 0 to 3, rather than the strings (hello, world, foo, and bar). No, "x" is actually each item in the array, not an index. Cervantes wrote: So, your for loop iterates four times, and the value of x is the particular element of the array. If I'm correct in my deduction, that's neat.
![]() Correct. Cervantes wrote: I was just checking out the Ruby Library Reference and noticed each_with_index is listed as a "mixin". It also seems to be enumerable.
![]() First, define each.
Now you can obviously do something meaningless like:
Now, mix-in enumerable.
Now, try:
Or any of the other methods in Enumerable. The key to a mix-in is understanding that we build on our previous work. If we have a bunch of methods that all build on "each", for instance, and require nothing else, we can put them in a module that might look like:
Then we can simply include that in a class that defines "each" and get a ton of "free" functionality. |
Author: | Cervantes [ Thu Jul 14, 2005 7:51 am ] |
Post subject: | |
Thanks again, wtd. ![]() I understand this now. I like how you included that snippet from the Enumerable module; it's always nice to see the code of the methods that were made for you. |
Author: | wtd [ Thu Jul 14, 2005 4:38 pm ] | ||
Post subject: | |||
Cervantes wrote: Thanks again, wtd.
![]() I understand this now. I like how you included that snippet from the Enumerable module; it's always nice to see the code of the methods that were made for you. Keep in mind that I'm pretty sure Enumerable is written in C. ![]() What I showed you was an approximation. Of course, you can see how this works by creating your own mix-in.
|
Author: | Cervantes [ Fri Jul 15, 2005 6:55 am ] | ||
Post subject: | |||
Thanks for your insightful replies to the each_with_index tutorial. I understand everything, except I'm a little unclear to the workings of include. I assume include acts like copying and pasting everything inside the module to where the include line is. If it were to copy the module itself, you'd have to call it like this,
right? Thanks once again, wtd! |
Author: | wtd [ Fri Jul 15, 2005 2:07 pm ] | ||||
Post subject: | |||||
Cervantes wrote: I assume include acts like copying and pasting everything inside the module to where the include line is. If it were to copy the module itself, you'd have to call it like this
If you were to copy and paste the module into the class, you'd access the module as:
But you wouldn't be able to access the each_with_rand method that way.
So, you see, include is a tad more subtle than just copying and pasting. ![]() |
Author: | cahill [ Sun Sep 04, 2005 8:15 am ] |
Post subject: | |
What is HTTP proxy server? HTTP proxy server is a proxy allowing to work on the Internet with HTTP and (not always) FTP protocols. It can carry out caching of information downloaded from the Internet.Now HTTP proxy servers are the most widespread. Their support (ability to use them) is included into many programs: browsers, download managers etc. However, their support is not realized at a level of an operating system ? in order to use them, you should configure all programs, which should use proxies, in an appropriate way. HTTP proxy servers have several anonymity levels. http://www.checkproxy.net |