Archive

Posts Tagged ‘Ruby’

This physically pains me

May 21st, 2009

Here’s a real excerpt (slightly modified to protect the stupid, and now fixed) from a web app I inherited. GRAAAAAAAARGH!

def encrypt_password
  ...
  self.salt = md5("#{Time.now.to_s}-#{login}")
  ...
end
 
def generate_confirm_hash
  ...
  self.confirm_hash = md5("#{Time.now.to_s}-#{email}")
  ..
end

Pro-tip for hiring managers: ask candidates to identify problems in the above code, and smack any who fail to do so.

Uncategorized , , ,

Easy-peasy symbolic computation with Ruby

January 3rd, 2009

Ruby is a wonderful language, largely deserving of the fanaticism surrounding it. There are a number of ways you can exploit its syntax to write concise, beautiful code. For example, to shuffle an array…

deck.sort_by{ rand }

…or to pick out certain elements of one…

deck.find_all{ |card| card.suit == Clubs }

…or to seamlessly cache computations.

def average_earnings
  @average_earnings ||= some_lengthy_computation
end

(Above, the ||= operator acts analogously to the familiar += operator. So if the instance variable @average_earnings already has a non-nil value, it is returned without any further computation. If on the other hand it is nil, then some_lengthy_computation is performed, @average_earnings is set to it, and returned.)

In addition, there are also a number of ridiculously short applications written in it, including a web server in 70 lines of code, a message board application in 500 lines, and its slightly more verbose successor.

In addition to these, I present a proof of concept of my own: Mathematica and Maple-like symbolic differentiation in about a hundred lines of code.

Read more…

Uncategorized , , ,