In chapter 10 we saw the basics to call a function. In this chapter we will cover more topics related to functions.
We have been doing a painful migration from Rails 2 to Rails 3 for several months at work, and refactoring some code the other day I had to do something in a non straightforward way, so I thought I’d share that.
Basically we had an action that would group several files into a zip file and return those zipped files to the user as a response. In the old code, a randomly named file was created on the
/tmpfolder of the hosting machine, being used as the zip file for the rubyzip gem, and then returned in the controller response as an attachment.
During the migration, we’ve replaced all those bespoken temp file generation for proper Tempfile objects. This was just another one of those replacements to do. But it turned out not to be that simple.
My initial thought was that something like this would do the trick:
But it did not. The reason for that is that the
openmethod, when used with the
Zip::File::CREATEflag, expects the file either not to exist or to be already a zip file (that is, have the correct zip structure data on it). None of those 2 cases is ours, so the method didn’t work.
So as a solution, you have to open the temporary file using the
Zip::OutputStreamclass and initialize it so it’s converted to an empty zip file, and after that you can open it the usual way. Here’s a full simple example on how to achieve this:
We saw in chapters 6 and 12 several control structures but we left out a usual one: the switch also known as select/case. In this chapter we will see how we can implement it in ARM assembler.
It may be suprising, but the ARMv6 architecture does not provide an integer division instruction while it does have a floating point instruction in VFPv2. In this chapter we will see usual ways to workaround this limitation with different techniques that can be used in specific scenarios involving divisions.
If you ever need to use the acts_as_list gem in Rails on a model that uses single table inheritance, here’s the snippet you need to use for the list methods to work if you want the setup done on the base model:
You’ll need to use the array syntax as neither the string nor the symbol versions will work. The symbol one assumes the column ending in
_id, while the string one will simply not allow you to evaluate the current object’s type.
It’d be nice to have a
lambdasyntax in future versions of the gem so you can inject code into the conditions.