I just published my first ruby gem on rubygems.org. It is a simple gem that is useful in case you need to find out if one number is divisible by another.


  9.divisible_by(3) # => true
  10.divisible_by(3) # => false
  12.divisible_by(3) # => true
  12.divisible_by(4) # => true
  15.divisible_by(4) # => false

Same can be done with

  Divisible.check(9, 3) # => true
  Divisible.check(10, 3) # => false
  Divisible.check(12, 3) # => true
  Divisible.check(12, 4) # => true
  Divisible.check(15, 4) # => false


  gem install divisible

For more info go to https://github.com/vlado/divisible

Simple Search Rails plugin

SimpleSearch brings simple search to ActiveRecord. It ads simple_search named scope that accepts query as parameter.

The idea is that you provide the query and plugin does the rest (splits query to keywords and compose where statement).

This can be very useful in case you just want to filter list of records by some query, you have autocomplete field, … or something similar.

Continue reading “Simple Search Rails plugin”

Ruby library for parsing, validating and formatting phone numbers

Tomislav Car has just released Phone, Ruby library for phone number parsing, validation and formatting. It should save you a lot of time if you need any of the following:

* you have area where users input phone numbers in many different formats
* output phone numbers in specific format
* you need to send SMS messages from your app
* …

Continue reading “Ruby library for parsing, validating and formatting phone numbers”

Radiant CMS, disable caching in development

I am working on extension for Radiant CMS. I wanted to disable caching for development, but wasn’t able to find right method for this, so finally I did it by putting this into my extensions activate method

if RAILS_ENV=="development"
  Page.class_eval {
    def cache?

This is more of a hack then a real solution, so please use comments to point me in the right direction. I’m using Radiant version 0.8.1.

Determining the number of days in a month

Here is a simple method that returns number of days for wanted month and year. If year is ommited current year is selected.

def month_days(month, year=Date.today.year)
  mdays = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  mdays[2] = 29 if Date.leap?(year)mdays[month]

Simple examples:

month_days(1)       => 31
month_days(2, 2007) => 28
month_days(2, 2008) => 29

Now, let’s assume we have a date from a database, and we want to find number of days in a month from that date.

month_days(date.month, date.year)

But with two more lines of code

Continue reading “Determining the number of days in a month”

Ruby Inject

You probably frequently have to iterate through a list and accumulate a result that changes as you iterate. Let’s take the following array:

nums = [1, 3, 5, 7]

If you want to find a sum of all the items in the array, you could write something like this:

sum = 0
nums.each { |n| sum += n }

This works fine, but it doesn’t look much elegant. Luckly, for us that wants to be elegant there is a inject method.
(See: Enumerable::inject)

sum = nums.inject(0) { |x,n| x+n }

This code does the same thing, but also looks cool. Inject acomplish this by using accumulator or accumulated value (x). At each step accumulated value is set to the value returned by the block. Here, we set accumulator initial value to 0, and at each step current value from array (n) is added to accumulated value.

If initial value is omitted, the first item is used as the accumulator initial value and is then omitted from iteration.

sum = nums.inject { |x,n| x+n }

Is same as

sum = nums[0]
nums[1..-1].each { |n| sum += n }

Another example.
We have array with five names:

names = ["michael", "ron", "scottie", "dennis", "toni"]

The following code:

string = names.inject("") { |x,n| x << "#{n} " }

Will output:

=> "michael ron scottie dennis toni "

You can get the same output with:

string = ""
names.each { |n| string << "#{n} "}

Now, let’s say you won’t to separate them with comma, and capitalize those that have more then four characters.
You can do it like this:

string = ""
names.each do |n|
  name = n.length > 4 ? n.capitalize : n
  n == names.last ? string << name : string << "#{name}, " 

Or like this:

string = names.inject("") do |x,n|
  name = n.length > 4 ? n.capitalize : n
  n == names.last ? x << name : x << "#{name}, "

In both ways we get the same result:

=> "Michael, ron, Scottie, Dennis, toni"

Technorati Tags: , , ,