Posts for: #Snippets

Faster JavaScript Trim

I’ve recently discovered the very interesting Steven Levithan post about the JavaScript’s trim function of the String class.

So, here my version:
function trim13 (str) { var ws = /\s/, _start = 0, end = str.length; while(ws.test(str.charAt(_start++))); while(ws.test(str.charAt(--end))); return str.slice(_start - 1, end + 1); }

More numbers, please!

I tested my function against the benchmarking page of the original post. Note: times are expressed in MS and are the average of ten executions per browser. Update [2008-05-29]: I re-runned all the tests and updated results, because Steven noticed that test suite was wrong (few whitespaces).

[]

Ruby: XML Parsing With SAX

SAX is an event-driven parser for XML.

It sequentially reads the xml and generates special events. So, if you want to use SAX, you should implement the code to handle them. It’s quite different from the DOM model, where the whole xml is parsed and loaded in an tree. As you can see, the first approach is more difficult than the DOM one. Why we should use it? Depends. If you want to extract certain informations from a big file, probably you should choose a SAX implementation, in this way you can avoid the initial DOM loading overhead.

The Ruby XML Library

The Ruby core library has a built-in XML parser (both DOM and SAX) called REXML, but it’s terribly slow, it’s highly advisable to use libxml. It’s a binding to the popular library from Gnome and it was released as gem.

[]

Ruby: How To Avoid A respond_to? Call

I’m writing this post as contribution to the Campagna Anti-IF (Anti-IF Campaign).

Problem

I’m developing an internal Rails plugin for widgets, it provides a class called Widget (really unconventional :-P), and each widget should inherit from it. The actual implementation provides a callback called before_render, that allows to add some logic to a widget, if implemented it’s called before the widget rendering.

Ruby doesn’t have abstract methods, so I have to check if the subclass has the implementation of mentioned method:
# Rendering code.. before_render if respond_to? :before_render # ...

[]

Rails: How To Create Custom Validations

Often our model objects leaning toward to be confused or noisy, due to validations DSLs. Imagine a class Answer, with an attribute, that should be exactly a string representation of a boolean. Ok, I know it’s an odd example, but: it’s trivial enough to make this example clear, and.. It happened to me to deal with this situation. :-P

class Answer %w( true false ), :message => "Should be exactly true or false." end

[]

Ruby: Read A File With One Line Of Code

This snippet shows how to read a file and puts all the lines into an array.

f = *open('file.txt').map {|l| l.rstrip} # => ["Hi, from the", "txt file."]

Explanation

The open method returns an IO object, that include the Enumerable module, now we can just use #map (or #collect).

The splat operator is only a sugar syntactical shortcut for map.

[]