Personal blog of Jack Casey.
RSS icon Email icon Home icon
  • Ruby on Rails Tip: .blank? .present? if and unless

    Posted on March 30th, 2011 beetlefeet No comments
    I've long known about the rails convenience method .blank? but only recently learned about its twin: .present?
    Combined with the ability to use "unless" instead of "if" means you can really tweak your conditional statements for readability.
    I like to use "unless" over "if" when I expect the conditional to be false (and the statement to be executed) and I like to use ".present?" over ".blank?" when I don't necessarily expect the attribute or variable to exist.

    Some examples:

    render "no id specified", :status => :unprocessable_entity if params[:id].blank?
    # I don't expect to render the error, I do expect params[:id] to be available

    user.nickname.downcase! unless user.nickname.blank?
    # I do expect to perform the downcase, I do expect nickname to be available

    conditions << {:style => params[:style]} if params[:style].present?
    # I don't assume I'll add the condition, I don't necessarily expect the style parameter to have been passed

    I'm sure some people disagree, but I think those are about as readable as they get. They're also more writable in that you just write what you think rather than having to transform the expression with one or more levels of negation.

    I love how ruby (especially rails) can be so self documenting based on this sorts of almost natural language syntax.

  • Scott Pilgrim

    Posted on March 16th, 2011 beetlefeet No comments

    So this is a really out of context and late review because I just found it lying in my gmail drafts folder :) Thought I’d may as well send it on.

    Scott Pilgrim vs the World is pretty fun with some very awesome scenes and sequences. With some cool gamer in-jokes (I loved the the name of the band) and content (is that ninja rhythm game real? If not it really should be…) It obviously plays on the good feeling you get from understanding in-jokes. Thats fine by me. The characters were also pretty cool, none of them being overly annoying. I thought the fight scenes were actually really well done (esp the first), and more satisfying than most ’serious’ fight scenes I’ve seen recently. I also liked the fancy editing with the disjoint cuts and the hat gag. More of that would have been good.

    One thing I did feel was that the progression was slightly off or something. Some of the earlier stuff seemed more interesting and exciting than some of the later stuff, when I guess ideally you have the ‘coolness’ factor building throughout. I think I enjoyed the first half considerably more than the second. I guess that might be pretty subjective though. And the Katayanagi Twins sequence was great. Other than that I’m annoyed that Ramona didn’t even get to properly use her weapon (bam!) and that the final boss wasn’t worth 9001 points. (I guess that’d be asking a lot from a ‘gamer’ movie though)

    Anyway, if you know your Super Mario enemies and what a triforce is (knowing how to triforce isn’t required) you should probably watch Scott Pilgrim vs The World. I can understand people might find it a bit meh or silly, but I was fine with that. Also if you aren’t keen on Michael Sera you might not like it :P