Posted by Jonas Elfström
Mon, 05 May 2008 19:17:00 GMT
I've felt it and I've heard it from colleagues several times. Writing unit tests can be hard work. Especially adding unit test to an existing code base is, at best, cumbersome. Also it's one of those things with delayed gratification. Sometimes it's not even you that will benefit from them being there because the biggest win can be long down the road, when changes to the system has to be made.
Tests may seem to be isolated and it's even considered a good thing to keep them that way. Even so the tests of your application has a correlation to what the system aims to do on a bigger scale. This one of the things BDD focuses on. I think that one of the biggest advantages is that you in one process writes a specification and tests that ensures that the spec. is met. Testing becomes a natural part of the development process. This way it clearly shows that BDD and TDD are design processes and that it's certainly not all about adding unit tests.
Find out more about BDD on: http://behaviour-driven.org
It must be stressed that BDD is a rephrasing of existing good practice, it is not a radically new departure. Its aim is to bring together existing, well-established techniques under a common banner and with a consistent and unambiguous terminology.
For Ruby RSpec has almost become the de facto standard for BDD. The concepts Story, Scenario, and Test feels natural and the syntax is short and easy to read.
In languages like Java or C# the tests often becomes much more cluttered and some of that clutter is the extra code that comes with static typing. I believe that dynamically typed and overall dynamic languages like Ruby or Python could find a nice little niche here. They could become DSL's for testing.
RSpec is on it's way for .NET/C# via IronRuby and for Java via JRuby but don't hold your breath because they are still in alpha and beta.
.NET / C#
Testing .NET with IronRuby...
NSpecify => RSpec… well closer anyway
Java
Java Functional Testing with JRuby and RSpec
JtestR
Ruby
Slapp - A simple chat wall Merb tutorial. With nice exampes of using RSpec.
Behavior-driven testing with RSpec
ASP.NET MVC
ASP.NET MVC Test Framework Integration Walkthrough
MVC Preview - Testing
ASP.NET MVC Session at Mix08, TDD and MvcMockHelpers
Posted in Ruby, C#, Java | 2 comments
Posted by Jonas Elfström
Thu, 15 Nov 2007 21:38:00 GMT
Sometimes you do not want to show the database id for a row in the URL. The reason could be that you do not want someone to be able to scan through all the data.
One solution is to use GUID's but they have drawbacks and one of them is that they add a considerable length to the URL. The shortest URL-safe representation of a GUID I've seen is 22 characters but usually they are 36 characters.
Depending on how your id's are implemented a much shorter way could be to simply to encrypt them.
Here's a Ruby-example that Blowfish encrypts, Base64 encodes and URL-encodes an integer value. You can get crypt as a gem:
gem install crypt
require 'rubygems'
require 'crypt/blowfish'
require 'Base64'
blowfish = Crypt::Blowfish.new("A key up to 56 bytes long")
plainId=123456
encryptedBlock = blowfish.encrypt_block(plainId.to_s.ljust(8))
idForURL = URI.escape((Base64.encode64(encryptedBlock).strip))
decryptedId = blowfish.decrypt_block(Base64.decode64(URI.unescape(idForURL))).strip.to_i
The .ljust(8) is because Blowfish is a 64-bit block cipher and the Ruby-implementation does not pad the data itself.
The id in the URL in this case would be c2PSXWgky40=. Its 12 characters long (11 if you skip the equal sign) and that's 10 or 24 characters shorter than a GUID. Also there is zero percent chance of a collusion and if you want to you can even decrypt it.
This is not a super safe implementation but if you start your id's at a random and not too low number you are making it a bit harder for someone to crack the 56-bit key. Actually a truly random and at least 64-bit big number would be a better choice as it would have no connection to the true id at all. You would have to check for uniqueness before storing those in the database though.
Posted in Security, Ruby | no comments
Posted by Jonas Elfström
Wed, 14 Nov 2007 22:50:00 GMT
There were no cross-platform windowing toolkits for Ruby so _why made one and he calls it Shoes. Not even close to 1.0, it's already yummy in a chunky kind of way and since it came from _why I simply had to try it out. Something simple.
Shoes.app :width => 1024, :height => 768 do
corners = [ {:x => 256, :y => 10}, {:x => 12, :y => 378}, {:x => 506, :y => 378} ]
xpos,ypos,c = 256,10,0
srand
2111.times do
c=rand(3)
xpos += (corners[c][:x]-xpos)>>1
ypos += (corners[c][:y]-ypos)>>1
star xpos, ypos, 5, 10
end
end
The result.
Posted in Ruby, Math | no comments
Posted by Jonas Elfström
Fri, 25 May 2007 16:31:00 GMT
The Zodiac Killer was a serial killer in the late sixties and maybe early seventies. He sent a number of letters to the press, including four ciphers or cryptograms and only one of them has been solved. The killer's identity remains unknown.
Chris McCarthy has a nice page about the cipher and he also has an ASCII version of the cipher.
Here's a small Ruby hack that calculates the character frequency using the ASCII version of the cipher. Feel free to use it if you like to have a go at cracking it!
EDIT: At this page you can have a go at cracking it real-time. I am not convinced it's really a homophonic substition cipher since the frequency analysis shows that the 340 does not have a flat frequency distribution.
It would be nice to know what cryptographic literature was available for the public in northern California in the late sixties.
Posted in Security, Ruby | 2 comments
Posted by Jonas Elfström
Fri, 30 Mar 2007 05:33:00 GMT
In an earlier post the example code did find prime numbers. Recently I stumbled over a really cool regexp hack that also deals with primes. This is how you execute that regexp in Ruby:
puts 'Prime' unless ('1' * 43) =~ /^1$|^(11+?)\1+$/
Change 43 to whatever you like and you will get Prime as output if it's a prime number.
EDIT: As you can see in the comments Neil Kandalonkar explained how the regexp by Abigail works.
Posted in Ruby, Math | 2 comments
Posted by Jonas Elfström
Tue, 27 Feb 2007 16:01:00 GMT
One-way hash functions takes a message of any length as input and outputs a very large but fixed length number, called message digest or fingerprint. They can be used for "storing" passwords or as a signature that makes it possible to verify that you got the correct message.
MD5 got into problems over 10 years ago and SHA-1 could to be heading the same way. Until the new standard is published I would follow the crowd and recommend SHA-256.
Ruby
require 'digest/sha2'
quickfox="The quick brown fox jumps over the lazy dog"
Digest::SHA256.hexdigest(quickfox)
=> "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"
C#
using System.Security.Cryptography;
...
ASCIIEncoding byteConverter = new ASCIIEncoding();
string quickfox="The quick brown fox jumps over the lazy dog";
HashAlgorithm sha256 = new SHA256Managed();
byte[] hash = sha256.ComputeHash(byteConverter.GetBytes(quickfox));
crypt.Text = Convert.ToBase64String(hash);
Posted in Security, Ruby, C# | no comments
Posted by Jonas Elfström
Fri, 09 Feb 2007 00:11:00 GMT
Someone mentioned SETL and I didn't even know what it was so I googled it. Ended up at http://en.wikipedia.org/wiki/SETL#Sample_code:
Print all prime numbers from 2 to N
print({n in {2..N} | forall m in {2..n - 1} | n mod m > 0});
The notation is similar to list comprehension.
Interesting! In Ruby you could do that something like this:
N=42
2.upto(N) {|n| puts n if (2..n-1).all? {|m| n.modulo(m)>0} }
The SETL example at wikipedia actually iterates way too much.
N=42
puts 2
3.step(N,2) {|n| puts n if (2..n/3).all? {|m| n.modulo(m)>0} }
I realize this is not purist list comprehension, well actually it isn't lc at all. Ruby does not have lc in the same sense as Python and others, but you can do almost everything you can with lc with the fantastic collection of methods in Enumerable. If that isn't enough you could always expand Ruby with a more general support for list comprehension like described here.
Posted in Ruby | no comments