surround helper alternative in Haml

Written . Tagged Haml, Ruby, Ruby on Rails.

I’ve previously blogged a simple alternative to the Haml succeed helper.

Today I wanted to put a link in parentheses. There is a surround helper, but that syntax isn’t pretty.

This is what I do instead:

1
2
3
%li
  =h item.name
  = "(%s)" % link_to_remote("x", item, :method => :delete)

The code

1
"(%s)" % "foo"

in Ruby is short for

1
format("(%s)", "foo")

or

1
sprintf("(%s)", "foo")

and will simply return

(foo)