improved ex2 (ruby)