Excel Series Decoder in Ruby
A co-worker of mine was writing a little script to parse an excel file that contained various network addresses and run various test cases against it. He was stumped on the algorithm how to decoded excel headers to specific indexes. While at first glance this looks like a simple problem, it ended up being more difficult than we thought. My co-worker approached the problem using a procedural approach, this ended up yielding a few nasty loops…far from elegant. The end result ended up being rather easy once the approach was modified to use recursion.
My Solution
require 'test/unit'
def to_excel(i)
case i
when 0
return ''
when 1..26
return ('A'..'Z').to_a.at(i - 1)
else
q, r = (i - 1).div(26), (i - 1) % 26
return "#{to_excel(q)}#{to_excel(r + 1)}"
end
end
class ExcelNumberSeriesTest < Test::Unit::TestCase
def test_simple
assert_equal 'A', to_excel(1)
assert_equal '', to_excel(0)
assert_equal 'Z', to_excel(26)
end
def test_doubles
assert_equal 'AA', to_excel(27)
assert_equal 'AB', to_excel(28)
assert_equal 'AZ', to_excel(52)
end
endIn Search of the Perfect Display
To accompany my recent Mac Pro purchase, I’ve been looking for a large format LCD display. I’m tired of my dual 19” setup, two screens are far less usable than 1 large one. While the AAPL screens are undoubtedly expensive, people usually compare AAPLs to oranges while giving cost figures.
All LCD screens are NOT created equal
The modern LCD screen is manufactured using 3 primary technologies:
- TN Film (Twisted Nematic + Film)
- MVA &t; PVA (Patterned Vertical Alignment)
- IPS (In Plane Switching)
So what is the big deal? It is all about trade-offs.
TN Film
TN Film LCD panels are the cheapest panels around. Look at any sub $400 24” LCD and you’re likely to find a TN Film panel inside. On the negative side, viewing angles are usually subpar when compared to the other LCD technologies. Usually about 10 degrees worse than an S-PVA panel. These panels have worse color presentation, often they only have 6-bit color depths versus 8-bit color. Many TN Film panels exhibit a purple hue throughout. On the positive side they have the best refresh ratings, often in the in 2-5ms range.
Common TN Film panels include:
- Any Samsung with a B in the model number (Samsung 245BW)
- Any Dell with an E at the beginning (E248WFP)
- ViewSonic Optiquest Series Q241wb
So far we can see TN film is less than desireable for anyone doing a significant amount of design work or if you’re a color snob (such as myself).
MVA and S-PVA
MVA (Multi-Domain Vertical Alignment) and S-PVA (Patterned Vertical Alignment) panels present a significant bump in quality over TN Film panels for a modest increase in price. In my opinion you should avoid TN Film and find an MVA or S-PVA panel. They offer an increase in brightness and viewing angle in exchange for a slightly higher screen latency.
Common MVA and S-PVA panels include:
- Samsung T series (245T, 305T)
- Dell 2407WFP
- BenQ FP241W
IPS
IPS is king when it comes to color representation. IPS panels exhibit true 8-bit color depth at the expense of latency, cost and contrast. If you are a fanatic about the color of your screen then IPS is the only way to go, unless you want to revert back to your old CRT.
Common PPS panels:
- Apple Cinema Displays
- Dell 3007WFP, 3008WFP
All it all, unless you absolutely must have the perfect color representation on your screen S-PVA or MVA is the way to go. Many of these panels can be had for near $450 price points. Be cautious when researching a screen, many times manufacturers will stop producing models that use the better S-PVA screens and replace them with the cheaper TN FIlm panels. This has happened with many models such as the Acer AL2416WBsd.
Compiling Erlang Applications with Rake
The irony of Rake is that Ruby really doesn’t need it. This is not to say it isn’t useful to Ruby projects, quite the contrary. Where Rake shines is in building software for applications that require byte code and object code, such as Java or C centric projects.
One such language requiring byte code compilation is Erlang. The design of the runtime environment used by Erlang should be familiar to most Java or C# developers. Erlang uses a byte code compiler (erlc) and various application meta-data files (.app, .rel, .config). For my development work I use Erlide, the Eclipse based Erlang IDE. The standard project layout for an application created in Erlide is:
- project
- ebin
- include
- src
When creating a Rake file for Erlang projects, I ran into a few problems with compiling the source into the separate ebin directory. Namely, dependencies where not being constructed as desired yielding a full rebuild every time I ran the build script! In the following example I dynamically create the file rules so that the bytecode (.beam) files will only be recompiled if the source (.erl) file is changed.
require 'rake'
require 'rake/clean'
CLEAN.include(['ebin/*.beam', '*.dump'])
SRC = FileList['src/**/*.erl']
BEAM = []
SRC.each do |fn|
BEAM << dest = File.join('ebin', File.basename(fn).ext('beam'))
file dest do
sh "erlc -o ebin #{fn}"
end
end
namespace :erlang do
desc "staring ermail"
task :run => [:compile] do
sh("erl -noshell -pa ebin -s my_mod start")
end
desc "run tests"
task :test => BEAM do
sh("erl -noshell -s test_my_mod test -s init stop")
end
end
task :default => [:compile]
task :start => ['erlang:run']
task :compile => BEAMJava vs Ruby
Forgive me, as this will most likely spark deep seeded hatred from those forced to navigate the deep dark world of Java development. I have managed to break free from it for sometime now, enjoying my Ruby bliss of block passing and metaclasses. Just when I thought I was out, they pull me back in. They, being work. Why can’t we all develop with a language so pointed and terse.
The Java Version
StringBuffer subType = new StringBuffer();
for (String token : tokens) {
// pretty print the tag
char[] c = token.replaceAll("flag", "").toCharArray();
// make first char upper case
if (c.length > 0)
c[0] = Character.toUpperCase(c[0]);
subType.append(c);
}The Prettier, More Compact, Ruby Version
tokens.collect {|t| t.sub(/flag/, '').capitalize }.joinI can hear the snide remarks right now…save them. There are plenty more examples where that came from.
Deploying Java Applications with Capistrano
Capistrano 2, the fantastic sequel to the already superb Rails deployment framework, is an excellent solution to the otherwise mundane task of deploying Java applications.
Network security restrictions prohibit me from using the typically SCM -> Production server configuration. Next, I ran into a few problems uploading Jar files using the put command. Luckily Alex Gorbatchev, posted an example of how to use SFTP within a Capistrano deployment recipe.
I used his idea and adapted my rails recipe using SFTP deployment.
namespace :deploy do
task :update_code do
on_rollback { run "rm -rf #{release_path}" }
run "mkdir #{release_path}"
files = Dir.glob('lib/*.jar') + Dir.glob('dist/*.jar')
execute_on_servers(options) do |servers|
servers.each do |server|
files.each do |path|
logger.info "uploading #{File.basename(path)} to #{server}"
sftp = sessions[server].sftp
sftp.connect unless sftp.state == :open
sftp.put_file path, File.join(current_path, File.basename(path))
logger.debug "done uploading #{File.basename(path)} to #{server}"
end
end
end
finalize_update
end
end