Calamity Lane

Curious Code for Curious Coders

A Rails Developer Spends One Week With Merb (and HAML and JQuery and DataMapper)

Developers often talk about some hot new technology they plan on spending time with, but very rarely do they ever get around to it. I’m ordinarily in the same boat, but last week I had a new personal project crop up for a simple web application (5-6 models) which was an ideal candidate for testing out a slew of new tech that I’ve been meaning to get around to.

Merb, an erstwhile competitor to Rails, has been making a big splash lately. I decided to step out of my comfort zone of Rails/Acti* and into the wild and woolly world of young, cutting-edge frameworks. Since I’m undergoing the pain of learning a new framework, I figured I’d go whole hog and use Haml, jQuery, and DataMapper, as well.

Before last week, I’d never done much more than spin through the initial tutorials for any of these tools. That being the case, I can’t really speak to performance, deployment, or any of half-a-hundred other critical issues to consider when selecting a framework– this is strictly a shallow, first-impressions review.

I welcome comments and corrections to my observations, and suggestions for easing some of the new-developer pain.

If You Like Lyrical Code…

Osake reminded me this morning that I hadn’t posted this one up yet…

1
2
3
4
5
if (self =~ /pina coladas/ && Rain.caught(self)
    && !yoga.include?(self) && self.brain == 0.5)
  Personals.answer
  self.relationship = :train_wreck
end

Bank of America: Bad, Bad, Bad

So, yeah, this is going to be a little ranty; feel free to skip it if you like Bank of America, or just don’t feel like hearing a windy blowhard spouting bile.

I’m not going into the many reasons I decided to close out my Bank of America account, just the representative experience of actually doing it.

Code Lyrical Umpteen-something…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class MaryMoon < Vegetarian

  def initialize
    @ttl = 2_524_554_080
    doowop
    @loves = Miso.new
    @alone.ambivalence = false
  end

  def eat(type)
    doowop
    raise ArgumentError if type.is_a? Meat
    return sho_like() if type.is_a? Bone
  end

  def doowop
    Thread.new { 3.times { 'Mary Moon' } }
  end
end

As always, jump for answers!

Mysterious, Incomprehensible Capistrano Errors

So yesterday a co-worker and I were fighting fires in our Capistrano deploy scripts. We’d been updating a crufty monstrosity of a cap script, and were getting an error along the lines of:

uninitialized constant #<Class:0x7f3230382cf0>::Capistrano::Deploy

After half a day of rolling back every single change we’d made to the scripts in the last week, we discovered a small junk file with some test code we’d stuck in the ‘lib/recipes’ directory. It was getting included even though it was not ‘require’d anywhere.

PROTIP: CAPISTRANO INCLUDES EVERYTHING IN THE ‘lib/recipes’ DIRECTORY BECAUSE IT HATES YOU.

Also, it saves typing maybe three lines of code.

Berceuse Code Lyrical

Somewhat in the same vein as our last one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def Son
  def pray(beneficiaries)
    unless beneficiaries.uniq.size == EVERYONE.size
      raise(ArgumentError, "Prayers should include everyone!")
    end
    @prayed = true
  end

  def tuck
    if @prayed
      internal_temperature = 99.9
      @sin_free = true
    end
  end

  def sleeping
    unless @eyes_open >= 1 && @pillow.gripped?
      @anthropomrphic_personification = Sandman.enter
      @sin_free = false
    end
  end
end

def Sandman
  def enter
    Light.exit
    Night.enter
    Hand.take
    never_never_land(:off)
    self.new
  end
end

Brainmallows

I love these things. They’re all filler and no meat, like marshmallow sandwiches. Which are also delicious.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Jungle
  attr_reader :fun, :games, :everything_you_want

  def welcome(new_user)
    self.new(new_user)
  end

  def initialize(new_user)
    (@@names ||= []) << new_user.name
  end

  def find(needs_query)
    return "Whatever: #{needs_query}"
  end

  def receive_money(amount)
    # TODO: this may be returning an unwanted value
    # check with Jim in accounting
    return :your_disease if amount > 0.0
  end
end

Jungle.welcome(honey)

Answer after the jump.

Stop That

I’m compelled to highlight a gem I found in our codebase the other day. If you ever find yourself writing something like:

1
variable == value ? true : false

…go sit in the corner and think about what you did wrong.

Code Lyrical, for the Old Folks

1
2
3
4
5
6
7
8
9
10
11
12
13
def i(arg)
  case arg
    when older.reject(&:my_hair) && years.from_now >= MANY
      send(:valentine, :birthday_greetings, :bottle_of_wine)
  end
  door.lock if Time.now >= Time.parse('2:45')
end

def need(age)
  age <= TOO_OLD
end

alias :feed, :need