v0.1.0: New Syntax For Array And Hash Values

2010-01-20 00:00:00 -0800

Starting in Albacore v0.1.0, all Array and Hash values for any attribute on any task object, will have the same syntax options. This is a significant improvement over the various styles that hav shown up in the different task objects, previously.

The Overview and Background

A month or so ago, Scott Bellware suggested that we standardize our syntax for all Array and Hash values in the framework. You can read the full discussion on this over at the Google Groups discussion (starting here and moving here here). The end result is that we decided Scott’s suggestion was a good one, and we moved forward with the changes.

The core of the change is that we now have all Array and Hash values being set through methods, without the = sign, using the splat (*) parameters syntax for the method parmeter. The end result of this change is that you will no longer need to use the = sign to set the value of an Array or Hash. Rather, you can simply specify the values after the method name.

Changing The Array Syntax

The Hash syntax in previous versions allowed you to write code like this:

msb.target << :Clean
msb.target << :Build

as well as code like this:

msb.target = [:Clean, :Build]

This syntax, while functional, is rather obtuse. It’s not obvious which you should use, when, and to new-comers in using Albacore and/or Ruby in general, it is rather confusing.

The New Array Syntax

The standard that seems to be permeating the Ruby language is to use the *args syntax for methods that need Arrays and Hashes. With this new syntax in place, you will now want to write the above code like this:

msb.target :Clean, :Build

If you need to wrap an array around more than one line, you still don’t need to worry about the square brackets. Rather, you can use the optional parenthesis that all Ruby methods support.

To wrap your array values around multiple lines, us this syntax:

msb.target(
  :Code,
  :Build
)

Of course, you’re free to choose where you put the parenthesis, as long as it is conformant to the standard Ruby method call syntax.

Changing The Hash Syntax

The Hash syntax in previous versions allowed you to write code like this:

msb.properties = {:Configuration => :Release, :OutputDir => "..\build\"}

as well as code like this:

msb.properties[:Configuration] = :Release
msb.properties[:OutputDir] = "..\build\"

The first syntax here is rather obtuse, like the old Array syntax. It’s not obvious and to new-comers in using Albacore and/or Ruby in general, it is rather confusing.

The second syntax is not quite so bad, but still requires a significant ammount of syntax noise, having to remember the square brackets and = sign.

The New Hash Syntax

Like the Array syntax, the standard that seems to be permeating the Ruby language is to use the *args syntax for methods that need Hashes. With this new syntax in place, you will now want to write the above code like this:

msb.properties :Configuration => :Release, :OutputDir => "..\build\"

If you need to wrap a hash set around more than one line, you still don’t need to worry about the curley braces. Again like the Array syntax, you can use the optional parenthesis that all Ruby methods support.

To wrap your Hash values around multiple lines, us this syntax:

msb.target(
  :Configuration => :Release,
  :OutputDir => "..\build\"
)

Consistency Is Good

We’re hoping that the new syntax, being consistent across all task objects, will help to alleviate some of the issues and questions that have popped up in the past. We know that the initial drop of this syntax will cause some questions, but over time, it should be a stable syntax and should alleviate questions on how to set Array and Hash values.

… and just to be nice, I left a little = sign syntax in the mix, using some metaprogramming. You can still use an = sign if you really want to.

blog comments powered by Disqus