Damien Mathieu

Blog d’un développeur web

Ruby vs PHP : count the number of words in a text

décembre25

This is the first article of a serie of comparisons between PHP and Ruby.

Count the number of words in a text is much more complex than what you should think first.
Because a space doesn’t  mean a new word everytime. There can be interrogation points with spaces before and after for example. And an interrogation point isn’t a word.

Here, we’ll take the following definition for “word” :
Two or more characters with at least one alphabetical character..

In other words : at leat two characters with at least one letter of the alphabet.

Let’s count our words in Ruby :

class String
def nb_words
self.split(/[a-z]+/).size
end
end

To count the number of words, we only have to do :
puts "my string ! Great no ?".nb_words

Which will then give us 4 and not 6. “!” and “?” aren’t words.

Let’s now do the same thing in PHP.
Even though I don’t think it’s a good idea as there’s the function str_word_count.
function nb_words($string) {
return count(preg_split('[a-z]+’, $string));
}

To be used like the following :
echo nb_words('my string ! Great no ?');

Ruby/Rails : number of week days between two dates

novembre24

It’s very easy, in Ruby, to count the number of days between two dates.

date1 = 1.month.ago
date2 = Date.today
puts date2-date1

However, you might need to calculate, not all the days between dates. But all the week days. For example if you’re working on an extranet and you want to count all the working days :)

Vamos ! Let’s put in your code (wherever you want. But put it somewhere logical please) :
class Date
def weekdays_until(date)
return 0 if date <= self
(self..date).select{|day| day.is_weekday?}.size
end
def is_weekday?
self.wday != 0 && self.wday != 6
end
end

And to use is :
1.month.ago.weekdays_until(Date.today)

Cool no ? :)

What are we doint ?
We overload the object Date by adding to it two new methods :
- is_weekday? which gives us true if the day is from monday to friday.
- weekdays_until which gives us the number of days from monday to friday only.

If course it doesn’t count the holidays ;)

Note : the tricks used here, like 1.month.ago are only available if you’re in a Rails environment. Otherwise, you have to calculate this by hand.

Let’s kill the trollers

novembre20

A french guy, named Eric Dupin closed his blog, called presse-citron because he’s tired of all the trolls in comments there. It’s only temporary as he’s planning to come back. But I want to react on that action.
Not because I don’t support it, all the contrary.

Even though I don’t really like the guy, who fakes to be an expert to make money with his blog, I completely agree with this action he’s doing. I am also tired of all those trollers.

A lot of web users thinks they can do anything on the internet because on the first look, anyone seems to be anonymous. But this anonymity is as virtual as all the web is. And Eric isn’t the only one to manage newbies who are really good at criticism. But not constructive critisicm.

As an example, until last year, I was moderator on a french SEO forum, called WebRankinfo. There was there an ODP forum for which only utility was to provide informations to users and allow communication between editors and users.
Well this forum have been closed one year and a half ago because of those trollers who made it’s management unbearable.
I was moderator there at that moment. And the trolls were depressing like you can’t imagine from my editor point of view.

So Mr. Dupin, my only message is : you’re not the only one to have to manage that kind of message in permanence. Be brave. You’ll see with time, you’ll learn to ignore them and forget them 3 seconds after reading it.

Note : because of battery problems, there’s no photo at the end of my articles for now.

posted under Others | No Comments »

Rails : add a validation error

novembre3

You probably already know (otherwise, I highly invite you to read the Ruby on Rails bases) how easy it is to validate datas with our favorite framework :)

if myDatas.save
#The datas have been saved
else
#There's an error. The myDatas.errors contains it.
end

After that, you only need to add some validation parameters for the datas in the model.
But now, let’s guess others validations cases, a bit particular that doesn’t enter in the “normal” case.

You only need to create a new method which will come validate your datas (the example above is completely fake) :

def validates_roxitude(*attribute)
reg = Regexp.new '/^ruby(.*?)rox$/'
self.errors.add('rox', 'Hey, Ruby rox. You have to say it !') unless reg.match attribute
end

Here, we declare an error except if the field starts par “ruby” and ends by “rox”.

We only need to force the validation with this method for our field and Ruby rox ! ;)
class myModel < ActiveRecord::Base
validates_roxitude :myField
end

New cellphone

octobre30

I changed my cellphone two weeks ago. After having a Sagem myx6 during a bit more than two years, I wanted to show how geek I am.

So I decided to pick up the new Nokia e71.

With the appropriate subscription of course. One allowing me to have illimited internet access (and illimites SMS because I’m under 26).

And for now, I’m sincerely very happy of it. Whether with the cellphone funtionnality, the internet or the MP3 player, I couldn’t have found better :)
So if you want to pick up a new cellphone, you know which one I would advise you ;)

posted under Others | No Comments »

What is the #1 reason for a website to be denied on dmoz ?

octobre23

I’m glad to announce this first blog article written in english :)

Today, an article on a blog was written with an interview of a dmoz editor.
You can this article here.

Unfortunately, there are many incorrect things in this article. I want to explain one.

Question number four : What is the #1 reason for a site to be denied?
The editor says : “Trying to get listed in the wrong category”.

This is unfortunately (or fortunately, depending of the point of view) not true.
If a website is incorrectly submitted, we highly invite the editor not to delete it. There are several options we give to you if you’re an editor and you see a misplaced suggestion.

  • Ask a fellow editor, through feedback.
  • Ask all the others editors, through the editors forum.
  • Move the website to a private category dedicated to that. Here it is.

But do not delete it. Many webmasters don’t even know that the category where the website they submitted isn’t appropriate.
So by deleting it, you’re not helping anyone.
Neither the directory’s users, who will miss a possibly interesting website because it’ll not get listed.
Neither the webmaster who will re suggest the website in your category.
Neither you who will have to theoretically delete the website in your category indefinitely.

So now, the real answer. What is the #1 reason for a site to be denied?
Well I guess you only have to read this page to get the answer of this question.

We do not list websites without interesting content. Any website that is clearly a MFA, constituted of affiliations or doesn’t have unique content will not get listed.

There isn’t any global deletion reason which is highly more used everywhere. It depends of the category.
In some categories, we’ll have a lot more of misplaced websites which the editor will have to move to their appropriate category.
In others, we’ll have much more MFAs or affiliations. But no global rules.

Rss Feeds

En Français
In English