You’ve probably already found some Ruby symbols, often used as hash keys.
Example :

{:foo => 'bar'}

:foo is a symbol. ‘bar’ is a string. But we could do :

{'foo' => 'bar'}

So why use symbols ? Let’s suppose the following case :

x = :sym
y = :sym

(x.__id__ == y.__id__ ) && ( :sym.__id__ == x.__id__)

This comparison will return true.
One same symbol will always be the same object in memory in your whole application. But that’s not a problem as symbols are not mutable.

And the same with strings :

x = "string"
y = "string"

(x.__id__ == y.__id__ ) || ( "string".__id__ == x.__id__)

Here, x, y and « string » are three different objects with different __id__. So 3 objects have been initialized in memory.

Now let’s take again our first hash with the comparison string/symbol.
With the symbols version :

{:foo => 'bar'}
{:foo => 'doe'}

We create 5 objects in memory : two hashes, a symbol and two strings.

With the strings version :

{'foo' => 'bar'}
{'foo' => 'doe'}

Here, we create 6 objects in memory : two hashes and 4 strings.

In an example case like this one it won’t change anything. But in a real application context, if you replace all your symboles by strings, you’ll need to increase the memory needed to run your application
So whenever it’s appropriate, stop using strings ans use symbols :)

When you get user information in console, you might, sometimes, need to get sensible information. Password for example.
Which can’t be displayed on the screen for security reasons.
The library Ruby Password allows you to do that quite easily.
However it implies you depend on this library. And that’s something I don’t wish. Particularly for something so simple.

The solution I’m suggesting here uses the linux features (not tested on windows. But who’s still using it today ? :mrgreen: ).

begin
    print "Username: "
    username = $stdin.gets.chomp

    print "Password: "
    # We hide the entered characters before to ask for the password
    system "stty -echo"
    password = $stdin.gets.chomp
    system "stty echo"
rescue NoMethodError, Interrupt
    # When the process is exited, we display the characters again
    # And we exit
    system "stty echo"
    exit
end

What are we doing ?
We start by asking the user’s nickname.

print "Username: "
username = $stdin.gets.chomp

This is not a sensible data and can be displayed. Nothing specific to do.

Then we ask for a password. Here we must hide it.

print "Password: "
system "stty -echo"
password = $stdin.gets.chomp
system "stty echo"

The « stty -echo » will hide every character that should be displayed on the console.
The « stty echo » displays them again.

Until there is works. Cool !
But somehowe we have a boring user, whom decides at the last moment to enter Ctrl-C to quit the program and finds himself in console with the -echo mode and doesn’t see what he enters in. We’ve just lost a friend.

Fortunately we have exceptions notifications and we already detect an Interrupt to exit the program cleanly.
Then we just have to display the characters again when there’s this interrupt :)

begin
    # ...
rescue NoMethodError, Interrupt
    system "stty echo"
    exit
end

Hop ! :)
And the project that uses this is glynn :)

I’m used to, when I want to update my local git repository with the distant one, to use git pull.
It works well. Until …

A few days ago, I wanted to release the version 1.0.0 of jesus. And to do so, I wanted to update the documentation.
The principle is simple. I use GitHub Pages. So I need a branch « gh-pages », which content is what’s online at GitHub for my documentation.
Whenever I commit on that branch, the online page is updated.

However my two branches master and gh-pages doesn’t have the same index. So they can’t be merged !
So I did

git pull origin gh-pages:gh-pages

Thinking « I get the content of my documentation branch ».
And I see a dozen of conflict errors ! WTF as we should say.

And I should have done

git fetch origin gh-pages:gh-pages

The difference between those two commands apparently similar is huge.
Git fetch only gets the datas. Git pull gets them and merges them with the current branch …
So when merging my two branches, as they don’t have the same base, it’s not cool.

Morality : you can git fetch when you want to update your local copy with the last distant commits. The branch on which you do the fetch will have the last commits.
But your working branch won’t be modified.
If you do a pull, not only the branch where you do the fetch will have the last distant commits. But your working branch will also be merged with those last commits.

After a second thought, fetch seems highly more recommandable that pull, which can break a repository pretty easily.

Some time ago, a PHP wanabee (berk), Romain was looking to make a web page screenshot only with command line.
Many solutions have been proposed. But my prefered one is Selenium. So I’ve decided to look closer into that.

First you need to have Selenium RC installed and launched.
It’s pretty simple. Download it, go to the selenium-server-1.0 and enter in command line

java -jar selenium-server.jar

Your Selenium server is started on the 4444 port, ready to be used !
You also need the selenium-client installed.

sudo gem install selenium-client

Your hard drive is now a bit less empty. We can start having fun with code ! :)
I want to do a screenshot of my portfolio.

We start with the magic and explain after.

require 'rubygems'
require 'selenium'

# We load Selenium
@selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://42.dmathieu.com/", 10000);
@selenium.start

# We go to the main page and take the screenshot
@selenium.open "/"
@selenium.capture_entire_page_screenshot(File.expand_path(File.dirname(__FILE__)) + 'screenshot.png', '');

# We unload Selenium
@selenium.stop

We load the required libraries. Not complicated. We only need Selenium.

require 'rubygems'
require 'selenium'

Then we load Selenium, indicating the URL we wish to visit and the browser with which we want to visit it.

@selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://42.dmathieu.com/", 10000);
@selenium.start

We load the page, take the screenshot and save the created image.

@selenium.open "/"
@selenium.capture_entire_page_screenshot(File.expand_path(File.dirname(__FILE__)) + 'screenshot.png', '');

And we don’t forget to free the memory.

@selenium.stop

And then the magic happens. Our beautiful screenshot (of the entire page, not only the screen) is then generated.

You’ll notice that javascript is executed (try to deactivate it on your browser, you won’t see my email on the page anymore).
And the render is what we have in the browser.

So who’s ready to start an open source project to generate websites thumbnails using Selenium ? :p

I love Git and GitHub. All my open source projects are hosted there (even though there isn’t a lot of them).
However I see one huge problem with it. Let me explain !

Today I falled on fetcher. One project that I was following since a few months.
But I forgot it and was happy to find it again and to think « great library. Let’s try it ».
So I take a look at the documentation and I see … It’s only a rails plugin. Not cool. I don’t want to parse emails on an IMAP server with Rails.
I want to do that in console and add the mails in a database.

So I think « let’s fork the project and make it a gem ». But as I don’t have a lot of time, I say : « what if someone else already did that before ? »
Then I look at the project’s network. And I see several other forks, two of them having already ported the library to a gem.

And here is the problem. The original project doesn’t seem to be maintained anymore. Other people have submited several patches to it.
But nothing takes all those patches to do one single and great project. And that’s a problem that appears on several other projects.

For some professional reasons, I’m currently taking a deeper look at CouchDB.
And particularly CouchREST. And here comes the nightmare !
There’s not one, not two but three active repositories for the project.mattetti’s, jchris’ and couchrest’s.
The original one is jchris. The other two ones are forks.
But apparently, the two ones are development versions (the forks of the two main developers ?). And the third one is the stable integration.
Cool, but not readaly at all.

I’d see two solutions for these two problems with GitHub.
For the couchrest problem, please projects owners, update your project description to explain what this repository is about.
Say this is only a development branch. Or it’s the stable branch and we should clone it and not your unstable testing repo.

For the fetcher problem, it seems to me to more like an architectural problem on GitHub.
Being able to move an original project from one place to an other (say « this project isn’t a fork. But this one is ») would change a lot of things.
We could, with the owner’s consent (or the github’s staff approval if the owner is unreachable) change the original project repository to some other place.
That’s called communication. If you see a project, you go to the original one, to download it and not a fork that might break everything.
But when it’s not the original that you must use but one specific fork, you’re not Mrs Irma and you can’t guess which one to use magically :)

Yes, this is a hidden message to defunkt, mojombo, tekkub, kneath or any other GitHub staff member if you read me.

A few weeks ago, I started to look at god. It’s a great tool to manage a machine’s processes.
However I noticed one big problem: checking the status of god via SSH is everything but cool.

So I though why not start something, an interface between god and us, poor humans down there ?
And there was born (not in a manger and not from Mary) jesus.

It’s goal is pretty simple: provide you with a web interface to view the processes managed by god.
For example, here’s a screenshot of the version 0.0.1 (we’re currently in 0.0.2).

Jesus is a Sinatra app and must be started as the same user than god (mongrel and unicorn allows pretty easily to start processes as root).
It sill needs to be quite improved. But you can already use it and view all the managed processes.

Feel free to report any bug and to fork the project and contribute to it :)

When you’re looking to create charts in Ruby, you might spend quite some time searching a cool tool.
I’ve been, for example, falling on gruff this morning.

But the graphs created with that kind of tools are always dependant of some libraries, and many times, they don’t look very nice.
So I’ve been working on something new, using something not new :mrgreen:
You’ve probably already heard of Google Charts, which allows you to create some cute charts.

Now you can use it with Ruby and Rails with my new plugin, Ruby on GChart.
You provide your datas, the type of graph you want and it gives you the URL to see the graph :)
You can even save this graph to a file and use it locally.

Here’s an example of what you can do :

GoogleChart::Chart.new({
    :type => :line,
    :height => 200,
    :width => 500,
    :encoding => :simple,
    :datas => [70, 25, 50, 35, 150],
    :labels => ['First Label', 'Second Label']
}).to_url

It supports simple and text encoding (with the parameters :simple and :text). No extended encoding for now.

However it supports muliple graphs in multi levels array.
Example :

:datas => [[5, 10, 15], [10, 15, 5]]

You can put as mush graphs as you wish of course.

So what do you think of it ?

I’m a bit obsessed by my applications statistics.
So on many of them, I build tools allowing me to generate graphics with several evolution indicators of theirs datas over time.
On RefStats for example, this graphic shows the evolution of the number of positions; the number of users; of websites and the average execution time for the crawler.

Moreover, you probably already know the rails commande rake stats which gives some statistics datas on your application code.
Number of code lines, number of test lines, …
The class that builds it is located in the railties.
Unfortunately this class isn’t abstract at all. It shows directly the datas to the console. Not very cool to enter them to your database.

So I’ve taken this class to update it and allow you to get those datas in an abstract way and use them in an application :)
So let’s me introduct you to Rails Code Statistics (yeah my projects names are always very original)

The principle is pretty simple.
You instantiate the class :

stats = CodeSearch.new

Then you can the datas as an hash :

p stats.to_h

As a json element :

p stats.to_json

Only the total statistics

p stats.total

Only the detailed statistics

p stats.statistics

And then do whatever you want with them :)
For me for example, I have a rake task which is executed automatically once a day and puts the number of code and test lines to my database.
Then I only have to manipulate this database to build a graph.

Note : the version above will only work if you’ve made your tests with RSpec.
If you use Shoulda, I let you do it yourself :mrgreen:
And if you use Test::Unit, I invite you to try RSpec. You will not want to do anything else after :mrgreen:

You probably don’t :)
RefStats was my end of studies project. However I’ve decided to keep maintaining it even after losing my student status.

It offers you statistics about your ranking on search engines (Google in many languages and Yahoo!).
And it’s available in English since the last friday :)

The big difference between RefStats and any other ranking statistics is that we provide you a full and free API to manipulate your account and get the statistics.
So you can fully integrate the tool to your extranet or any application without getting worried with the crawling the result pages yourself :)

Feel free to subscribe and try it. And please drop a message either in comment or in the forum if you have any comment.

After my last week post « help me make my playlist » (fr), we’ve decided with Romain, to develop the concept.

It’s, for now, only a simple wordpress which will have two posts each week :
- One on monday morning, that’ll invite any user to share his prefered musics.
- One on thursday evening, giving the spotify link to the week’s playlist.
And on friday, you can listen and enjoy a great playlist :)

It’s on Friday Playlist. And you’re highly invited to participate.

 
Fork me on GitHub