sharp bites

standing on the shoulders of giants

git started #1. Using git with a svn server

So, you already know that svn sucks and you want to switch to git because you know it’s awesome, but you are stuck in a team environment of svn-zombies.

What can you do? You can try to convert all the non-deads at once… or you can use git-svn and get them into git one by one.

git-svn let’s you interact with a svn server using git as your client. Some git GUIs support git svn. On Mac OSX you can use GitX(L) or SourceTree. Here is how to do it using the commandline.

To get a copy of your svn repo (assuming you have the de facto standard structure of trunk, branches and tags dirs) do:

git svn init -s http://path/to/your/svn/project

From here, use your local repo as usually, i.e. do some hacking, add your files and commit them. Rinse, repeat.

git add somefile 
git commit
...

When you are ready to commit your changes, get the changes from the svn repo:

git svn rebase

and commit your changes:

git svn dcommit

That’s it!

Change

“You never change things by fighting the existing reality. To change something, build a new model that makes the existing model obsolete.” R. Buckminster Fuller

Setting up a ruby testing environment on Linux

Now that we have setup our ruby environment on Linux, we are going to create a setup for testing purposes by adding some useful gems.

For that, we will use a feature from rvm called gemsets. A gemset is an independent setup for your environment. That means you can have many environments with a different set of gems (hence the name) installed independently on each one (even with different ruby versions).

Usually, when working on a project, you would create a gemset specific for that project, but in this case, we are going to create a gemset for practicing tdd, doing katas, coderetreats, etc. If you plan to use rvm in a serious project (which you should), please read first about rvm best practices.

A brief introduction to gemsets
For each version of ruby, rvm creates two gemsets, the default, empty gemset and the global gemset. You can list your gemsets with:
rvm gemset list
Gems that are installed to the @global gemset are shared to all other gemset for that ruby. We won’t install anything here. Instead we will create our own gemset and put everything there.

Create a tdd gemset:
rvm gemset create tdd
Switch to that gemset:
rvm use gemset 1.9.2@tdd
If you want to make this your default gemset (the one that will be loaded by default) you can do that by doing:
rvm use gemset 1.9.2@tdd –default
Verify you are on the new gemset:
rvm gemset name
You should get:
tdd
Gems for testing that rock
You can install gems for your current gemset using:
gem install
To do unit testing in ruby, rspec is a great choice.
gem install rspec
To start using it, create an rspec file, like example_spec.rb inside a spec folder on the root of your project:
describe "something" do
it "does something that passes" do
5.should eq(5)
end
You can then execute it by running:
rspec spec/
For a nicer, colored output, pass in the –colour flag to rspec
rspec spec/ –colour 
Or create a .rspec config file on your project root (or in your home dir if you want to always use it), to tell rspec to use the colored output by default
echo “–colour” >> .rspec

Excecuting rspec by hand all the time gets boring quickly, and it turns out computers are quite good at doing tasks repeteadly, so you can instead use autotest, a cool gem from the ZenTest suite that will automagically execute your tests when you save your files.
gem install autotest
Execute it from your project root.
autotest –style rspec2 –quiet
The –style flag tells autotest to look for rspec kind of tests. The –quiet flag removes a bit of noise you probably don’t care about.

If you have created the .rpec file on your project root, you can omit the style flag and autotest will still find the tests.
autotest –quiet
If you are really lazy like me, you can instruct autotest to be quiet by default by creating a .autotest file in your home:
echo “Autotest.options[:quiet] = true” >> ~/.autotest
So whatever option you want to use, let’s try it. Modify your rspec file and save it. Awesome, isn’t it? But wait, there is more!

We’ll add a nice gem called test_notifier. This is the topping of the cake. It will notify your OS with the result of your tests.

In order to it on GNOME, we will need to install libnotify:
sudo apt-get install libnotify-bin
gem install test_notifier
Configure autotest to use test_notifier:
echo ‘require “test_notifier/runner/autotest”’ >> .autotest
Or add the following to your rspec/spec_helper file if you want to use it directly with rspec:
require “test_notifier/runner/spec”
Now you should get a nice notification when you save your file.

Setting up your ruby environment on Linux

Here are the instructions on how to set up a ruby environment for Linux, in case you want to start playing with it.

NOTE: Don’t use the packages from your distribution. They are outdated and will cause more harm than good. Use one of the methods below.

The quick and easy way
If you are planning to use Rails anyways, you can use the script from railsready.
It will install rails, passenger, and a shitload dependencies it need for everything to work smoothly.
You can install it by running:
sudo wget –no-check-certificate https://github.com/joshfng/railsready/raw/master/railsready.sh && bash railsready.sh
The script will ask if you want to build Ruby from source or install RVM. Choose RVM.

It will download around 50MB of libraries from the repositories and then download and compile rvm, ruby and download again some other gems, so it will take some time. Don’t worry, you don’t have to do anything, just wait.

The almost-as-quick, almost-as-easy way
If you don’t intend to use rails and don’t feel like installing all that stuff, use the following method. It’s basically doing the same thing as the previous script, except you don’t need so many dependencies, and you won’t install rails or passenger.

Installing RVM

RVM (Ruby Version Manager) is a tool which allows us to easily install, manage and work with multiple ruby environments (interpreters and gems) on the same machine. This means we can create different environments with distincts versions, of ruby, rails, or whatever gem we want, without any headaches.

In order to install rvm, you’ll need curl, git and some compiler tools. We can get them with the following command:
sudo apt-get install git-core curl build-essential
To install rvm execute:
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
This command will install rvm in your home directory.
You need to include a line at the end of the .bashrc file to tell it to load rvm.
Follow carefuly the instructions on your terminal, specially the part about converting the return to an if statement
”[ -z “$PS1” ] && return”
NOTE: The railsready script doesn’t do this, and just appends the line loading rvm at the end of the file. RVM maintainers recommend otherwise, so you better do it their way.

If you find any problems, check rvm site troubleshooting information (section ‘Trhoubleshooting your install’)

Finally reload your envirnonment executing:
source ~/.bashrc
Installing ruby
Now you have rvm installed and running, you can proceed to install ruby. You’ll need some packages first, the following command will tell you which:
rvm notes
In my case, these were:
# For Ruby (MRI & ree)  you should install the following OS dependencies:
sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf
Now, we can install ruby 1.9.2:
rvm install 1.9.2
This will take a while, since it is dowloading ruby sources and compiling them.

You can check ruby is all fine and dandy by running
ruby -v
You should get something similar to:
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-linux]
On the next post I will explain how to setup your environment for testing using rvm.

New Year resolutions

Here are my resolutions for the present year:
- Read LESS. Yes, less reading, more doing.
- Keep learning ruby.
- Practice! I’m resolved to do a kata EVERY day, one kata each month. Wanna join? Use the hashtag #12months12katas.
- Rediscover enjoyment, on many aspects. This is a tough one, since I first need to find direction, then courage to act in consequence.

12 months, 12 katas: January. String calculator

As recommended by @plagelao on twitter, the kata for January will be String Calculator.
Here are the specs:

  1. Create a simple String calculator with a method int Add(string numbers)
    1. The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example “” or “1” or “1,2”
    2. Start with the simplest test case of an empty string and move to 1 and two numbers
    3. Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
    4. Remember to refactor after each passing test
  2. Allow the Add method to handle an unknown amount of numbers
  3. Allow the Add method to handle new lines between numbers (instead of commas).
    1. the following input is ok:  “1\n2,3”  (will equal 6)
    2. the following input is NOT ok:  “1,\n” 
    3. Make sure you only test for correct inputs. there is no need to test for invalid inputs for these katas
  4. Allow the Add method to handle a different delimiter:
    1. to change a delimiter, the beginning of the string will contain a separate line that looks like this:   “//[delimiter]\n[numbers…]” for example “//;\n1;2” should return three where the default delimiter is ‘;’ .
    2. the first line is optional. all existing scenarios should still be supported
  5. Calling Add with a negative number will throw an exception “negatives not allowed” - and the negative that was passed.if there are multiple negatives, show all of them in the exception message
    stop here if you are a beginner. Continue if you can finish the steps so far in less than 30 minutes.
  6. Numbers bigger than 1000 should be ignored, so adding 2 + 1001  = 2
  7. Delimiters can be of any length with the following format:  “//[delimiter]\n” for example: “//[***]\n1***2***3” should return 6
  8. Allow multiple delimiters like this:  “//[delim1][delim2]\n” for example “//[*][%]\n1*2%3” should return 6.
  9. make sure you can also handle multiple delimiters with length longer than one char

Enjoy!

Greed game koan

@jjballano recently posted his version of the Greed game, as part of his learning experience with Ruby Koans. As I am following that very same path, I thought it could also be worth for me to share my implementation of Greed for people to comment on it.

The code could surely be much prettier since I am still trying to get my head around ruby block-passing, among others, so comments (and patches!) are welcome.




UPDATE: The code seems to update automatically to the latest version on github, you can find the original here.

I’ve read: Peopleware

‘Peopleware’ is one of those classic books of our profession, a must-read. Unfortunately, it’s not easy to grab a copy of it on the stores (it may have been discontinued, I’m not sure). After a failed attempt to borrow it from Jose Manuel Beas, a few weeks ago, I finally got the chance to read it (thanks Rubén!).

All I can say is, if you have the chance, read it! Just so you can have a glimpse at it, here are some notes I took from it. Most are direct quotes, although I reworded a few to synthesize.

Productivity
As long as workers are crowded into noisy, disruptive space, it’s not worth improving anything but the workspace.
Experiments showed projects without deadlines had the best productivity.
Better performance can be explained entirely by more effective ways of handling people, modifying the workplace and corporate culture.
Developers main work is human communication to organize the user’s expressions of needs into formal procedure.
People won’t work better under a lot of pressure.
The manager’s function is not to make people work, but to make it possible for people to work.
Best people outperform the worst by 10 and the median performer by 2.5.
Years of experience are not correlated to productivity.
Interrupting flow not only causes disruption but frustration.
There are two types of work: Individual work is noise sensitive and needs flow. Group work is noise producing. Adapt your workspace to make both kinds of work possible.
Measure and try to maximize flow time, but don’t let managers access to data.
Ignoring the phone must be accepted by corporate policy.
Ask people about their workspace conditions, what affects their productivity.
Listening to music uses the right part of the braing, which is responsible for creativity.
Encourage teams to customize their space.

People
People who work for you will be more or less the same at the end as they were at the beginning, so they better be right for the job from the start.
Find the right people, make them happy, turn them loose.
Success or failure is in the cards from the moment the team is formed and the initial directions set out.
Corporate pressure is pushing towards the company average, encouraging you to hire people that look, sound and think like everybody else.

Hiring
(Tell about hiring a juggler) ‘Don’t you want to see me juggle?’
Make people bring their code portfolio for interviews.
Make candidates prepare an audition with the team 10-to-15 minute presentation on some aspect of past work.

Good organizations
Late promotion is a sign of health.
With a low and flat hierarchy, people at the lowest level have, on average more years of experience.
Strive to be the best
Grow a community feeling.
Focus on long term benefits.
Widespread sense that you are expected to stay
Invest in personal growth
Retrain your people
When you automate a system, you make it deterministic, so it looses its self-healing ability. A Methodology (capital ‘M’ here) can produce the same results on you organization.
To achieve convergence of methods, use training, tools and peer review.
Don’t declare something a standard until it is a de facto standard.
Hawtorne effect (experiment of raising and lowering the light) People perform better when they are trying something new.
You have to make non-standard approaches to the rule. Whatever standard there is should be brief and gentle.
Let a hundred flowers blossom and let a hundred schools of thought contend. (Mao Tse-tung)

Jelled teams
Productive teams = challenge + team interaction
They have momentum.
They have a goal
The purpose of a team is not goal attainment but goal achievement.
People on jelled teams are often so involved you have to remind them that what they are trying to accomplish is not winning a war.
Jelled teams have low turnover and strong sense of identity.
Money, status and position for advance matter a lot less. Enjoyment is an obvious sign.
You can’t make teams jell, just act to improve the odds.
Teamicide techniques:

  • Defensive management
  • Bureaucracy
  • Physical separation
  • Fragmentation of people’s time
  • Quality reduction
  • Phony deadlines
  • Clique control

Good managers provide opportunities for the team to succeed together. The best success is the one in which there is no evident management. The best boss is the one who can manage without the team knowing they’ve been managed.

Growing jelled teams:

  • Give people autonomy and responsibility
  • Let them work alone, without constant supervision
  • They break the rules when they believe in it
  • Let them choose mates and projects
  • Let them exercise their natural authority in their area of expertise.

Chemistry for team formation:

  • Make a cult of quality
  • Provide satisfying confirmation
  • Build a sense of eliteness
  • Allow and encourage heterogeneity
  • Preserve and protect successful teams.
  • Provide strategic but not tactical direction


Chaos (it is not that bad!)
Small amounts of disorder are benefitial
Pilot projects, war games, brainstorming, provocative training experiences, training, trips, conferences, celebrations, retreats,…

If your corporation is fortunate enough to have a self-motivated superachiever on-board, it’s enough to say “Define your own job”
“Free electrons” have a strong role in choosing their own orbits.

Being a Change Agent
Focus on one thing to change. Raise people’s consciousness of it, so they help you change it.
Be careful with motivational posters, they can have the opposite effect.
Fuck overtime.
Competition inhibits coaching.
Any action that rewards team members differently is likely to foster competition.
The success of the individual should be tied to the success of the whole.
People hate change. Any change.
Your enemies are blindly loyals and militantly opposed to change.
The fundamental response to change is not logical, but emotional.
Celebrate the old as a way to help make change happen.
Be aware that chaos is an integral part of change. Otherwise, you mistake it for the new status quo and will want to change back.
Change always involves chaos, it is necessary and can’t be shortcut. The more painful the chaos, the greater the perceived value of the new status quo.
Change won’t even get started unless people feel safe when they know they won’t be demeaned for proposing or trying a change.
Change only has a chance of succeeding if failure is also OK.

Organizational learning
Learning is limited by an organization’s ability to keep its people.
If the retrained people leave, investment is lost and learning is gone.
Successful learning organizations are always characterized by strong middle management. In order for a vital learning center to form, middle managers must communicate with each other and learn to work together in effective harmony.

Waste
The ultimate management sin is wasting people’s time.
Organizations have need of ceremony.
Ceremony is good when it fulfills a need for appreciation by the team.
Early over-staffing is waste.
Fragmented time is waste, it breaks flow.

Community
Building a community makes a difference, but requires talent, courage and creativity. And an enormous invest of time, to be, at best, the catalyst.