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.

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

Rss Feeds

En Français
In English