Space Vatican

Ramblings of a curious coder

Introducing Asset_symlink

Rails 4 made asset pipeline precompiling now lightning fast compared to rails 3.x. There were two reasons (that I am aware of) for the slowness in rails 3:

  • it would regenerate all the assets, each time (you can fix this via the turbo-sprockets-rails3 gem).
  • it used to precompile everything twice: once with the digests (the checksums in the filename) and once without.

The precompilation happened twice because your assets can reference other assets (eg a stylesheet with some image or font urls) so the difference between the digest assets and the non digest assets wasn’t just the filename.

Rickrolling With Ruby and Objective-C

In the session I presented at railsclub I finished off by showing how to use FFI to call into Objective-C (If you weren’t there on then my slides are online – I would recommend browsing through the section on FFI). I didn’t go into any detail because it was the end of what I think was a pretty technically intense session.

This definitely comes under the category of “doing something to prove it can be done” rather than anything more serious, but if you’d like to know how it works then read on!

Selectable Items in NSToolbar

NSToolbar has a mode where the toolbar items are selectable – this is commonly used to create preference windows, each item in the toolbar representing a section. If your delegate responds to toolbarSelectableItemIdentifiers: then the toolbar will allow these items to be selected.

You can do all this in IB (along with the rest of the standard NSToolbarDelegate stuff), since IB allows you to mark items as selectable.

The bit that confused me is that unless you also assign an action to the toolbar item (I’ve got mine pointing at a selectSection: method) then nothing will happen when you click the toolbar items, not even a change of selection state. Nowhere does this seem to be documented. I expected that even though I’d hooked up no actions and the toolbar wouldn’t be useful I would at least be able to play with the selection state. Hopefully this helps anyone else wondering why their toolbar isn’t working.

Direct to S3 Browser Uploads

The easiest thing to do with S3 uploads is have them go straight to your app server and then post them on to S3. This is a bit clunky if you don’t need any pre processing). Fortutnately you can post files straight to S3 from the user’s browser with an appropriately crafted form. Amazon have some documentation on this but there are a few details I thought were worth spelling out.

Ruby Truth Serum

One of the joys of ruby is its flexibility – there are very few things set in stone. Occasionally this can make things hard to figure out. For example if someone has overridden the class method (not that uncommon when using proxies), then you can waste a lot of time wondering why something that claims to be an array isn’t quite behaving like one. Once you’ve established this, how would you find out what the real class of the object is?

I came across a neat trick today for dealing with this:

1
2
3
4
5
6
7
8
9
class Suspicious
  def self.class
    Array
  end
end

object = Suspicious.new
object.class #=> Array
Object.instance_method(:class).bind(object).call #=> Suspiscious

What we’re doing here is using Object’s implementation of the class method and calling it with the object we have at hand. Nifty