Entries in Programming (99)

Saturday
Jan142017

Functional Programming Talks

In the past few months the following lectures have greatly helped my understanding of how algebraic structures can be used to design and construct functional programs:

Sunday
Jan122014

Tim Bray on Javascript

JavaScript is horrible.

> [5, 10, 1].sort();
[ 1, 10, 5]

Et cetera.

From here

Saturday
Mar162013

RVM SSL Certificate Errors

The Ruby Version Manager RVM is one of the key components in a Ruby development environment on non-Windows platforms.

Yesterday I wanted to install it on my Ubuntu laptop.  The recommended way of doing is to run the following command in a terminal:

curl -L https://get.rvm.io | bash -s stable

Unfortunately this gave SSL certificate verification errors.  Visiting rvm.io in Firefox and Chrome indicated that this was due to the site's security certificate having expired.

A little Googling revealed the recommended workaround which is to use Gihub instead of rvm.io:

curl -L https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer | bash -s stable
Sunday
Feb102013

Misleading minus signs in Cucumber progress format

I have been recently using the Ruby test tool Cucumber and came across a little annoyance.  In the progress format output there were unexpected minus signs:

  $ cucumber --format progress features/codebreaker_submits_guess.feature
  ---..........................................
  14 scenarios (14 passed) 42 steps (42 passed)
  0m0.117s 
  $

Normally minus signs would be used to indicate skipped steps but here they seem to be displayed for each scenario outline step whether it is skipped or not.

A little Googling revealed that this is a known bug that has not yet been fixed because of the awkward internal design of Cucumber.

Sunday
Dec302012

How to Inhibit Escaping in Rails3 ActionView Custom Helpers

I had bits of code like this scattered throughout my Rails3 views:

  <%= image_tag(thumbnail_image_path(@image.id)) %>

I wanted to enclose each image_tag call in a <div></div> pair to allow styling, so I wrote the following custom helper and placed it in app/helpers/application_helpers.rb:

  def thumbnail_block(image_id)
    "<div>#{image_tag(thumbnail_image_path(image_id))}</div>"
  end

I then replaced the calls to image_tag in my views with calls to thumbnail_block:

  <%= thumbnail_block(@image.id) %>

However, instead of displaying the images, this displayed the HTML code in the browser window.  Rails3 was automatically escaping the string produced by the new custom helper.

A search on Google lead me to this answer by Mike Fisher at StackOverflow and this post by Yehuda Katz at Rails Dispatch.  These gave me the solution: the custom helper needs to mark the string as html_safe before returning it:

  def thumbnail_block(image_id)
    "<div>#{image_tag(thumbnail_image_path(image_id))}</div>".html_safe
  end

Now my images display properly.