December 08, 2010
I just pushed a new version of my Diffy ruby gem.
This version does a better job when highlighting parts of lines in the html diff output.
Ward Cunningham pointed out to me that sometimes Diffy would get a little creative when finding similarities in between lines. For example, while technically correct Diffy 2.0.0 could come off as a bit of a know-it-all when it found one letter similarities between “Unescaped” and “Swallowed”.
- ==Links around Images==
- Images should link to absolute paths like ’/File:Picture.jpg’ instead of relative links (i.e. ‘File:Picture.jpg’).
- The later confuses my browser and causes it to link to file:///Picture.jpg, which won’t work at all.
==Unescaped html in nowiki tags==
- ==Swallowed html in nowiki tags==
The break tag in here is rendered.
- The break tag in here is not rendered as literal text despite being in nowiki and code tags. This differs from mediawiki’s behavior.
- “To create a line break put this <code><nowiki><br /></nowiki></code> in your markup.”
Version 2.0.1 suppresses this creativity a little bit for clearer output:
- ==Links around Images==
- Images should link to absolute paths like ’/File:Picture.jpg’ instead of relative links (i.e. ‘File:Picture.jpg’).
- The later confuses my browser and causes it to link to file:///Picture.jpg, which won’t work at all.
==Unescaped html in nowiki tags==
- ==Swallowed html in nowiki tags==
The break tag in here is rendered.
- The break tag in here is not rendered as literal text despite being in nowiki and code tags. This differs from mediawiki’s behavior.
- “To create a line break put this <code><nowiki><br /></nowiki></code> in your markup.”
Tagged with: ruby |
November 26, 2010
I just released version 2.0 of Diffy.
I wanted to make it dead simple to generate attractive diff output from within a ruby application.
Here’s how you use it:
$ sudo gem install diffy
$ irb
>> string1 = <<-TXT
>" Hello how are you
>" I'm fine
>" That's great
>" TXT
=> "Hello how are you\nI'm fine\nThat's great\n"
>> string2 = <<-TXT
>" Hello how are you?
>" I'm fine
>" That's swell
>" TXT
=> "Hello how are you?\nI'm fine\nThat's swell\n"
>> puts Diffy::Diff.new(string1, string2)
-Hello how are you
+Hello how are you?
I'm fine
-That's great
+That's swell
It also will generate nice html for you:
>> Diffy::Diff.new(string1, string2).to_s(:html)
- We were all having a good time.
- Soon it was very late.
“Time flies like an arrow” said I.
- “Fruit flies like a banana” I said.
- Then I left.
You can find out more in the README
Tagged with: ruby |
November 09, 2010
After we set up AboutUs’s search feature our (awesome) sysadmin wanted a redundant setup. He doesn’t like having to cancel his weekend if a box goes down. We talked about running an instance of HAProxy on all our app servers to load balance between two Sphinx servers, but that seemed heavy handed.
It turned out to be really easy to add this functionality to ThinkingSphinx (and the Riddle client it uses to talk to Sphinx).
Tagged with: ruby |
October 13, 2010
At AboutUs where I work we recently re-implemented the
site’s search feature. Behind the scenes the new search uses
Sphinx (a super-fast and scalable search engine) and
Thinking Sphinx (which allows easy
configuration and querying of Sphinx from within Rails).
One thing that makes Sphinx so much faster than alternatives like Solr is that
it communicates directly with your database when it is building its search
index. This is far faster than talking with your application layer, and
indexing the result of your application models’ methods. In our case we’re
able to reindex about 40 million records in around 4 hours. Solr used to take
days.
Tagged with: ruby |
August 03, 2009
A lot of people say that Ruby is a great languages for writing Domain Specific Languages (DSLs). A DSL is a highly abstracted programming language that gives you a natural and intuitive way to deal with a specific logical domain. They can serve as easy flexible APIs for programmers or enable clients to have control over the way a system deals with their business logic. There are lots of examples of DSLs in the Ruby world. Capistrano, RSpec, Thinking Sphinx, Rails’ Routing, just to name a few.
In this post I’ll look at some of the most common ways to create a DSL in Ruby.
A DSL for Defending Medieval Castles
A new client, Medieval Guards, Inc. specializes in guarding medieval castles and fighting off Barbarian attackers.
Tagged with: ruby |
July 09, 2009
I’m a little excited today because some code I wrote was accepted into the Ruby on Rails core.
It’s a patch I wrote to fix a tricky bug I ran into in ActiveRecord which can cause a call to one of your model’s attribute methods to sometimes throw a NoMethodError.
I came across this bug after upgrading a site I help maintain to Rails 2.3. This site allows users to download demos of software. Users have to specify which operating system they want a demo for, and this is stored in a database column named system.
Tagged with: ruby |
June 10, 2009
One of Rails’ selling points is convention over configuration, but most apps do need some configuration. database.yml is a great example.
Here’s some code that loads data into a ruby class from a YAML config file. Just like database.yml the configuration is specific to the rails environment you’re running in.
Tagged with: ruby |
May 19, 2009
Sometimes you want to decide whether or not to pluralize a string based on a quantity. For example, at the bottom of this article should it say 1 comment or 2 comments? It depends on how many comments we have. I was running into this in a lot while pair programming on one project, so Ian Smith-Heisters and I whipped up this simple helper which makes it easy.
class String
def smart_pluralize(num=self)
num.to_i.abs == 1 ? self : pluralize
end
end
Tagged with: ruby |